java
1import java.util.ArrayList; 2import java.util.Arrays; 3import java.util.Scanner; 4 5public class main { 6 public static void main(String[] args) { 7 ArrayList<String> fruitsPictorialPatternString = new ArrayList<String>(); 8 fruitsPictorialPatternString = new ArrayList<>(Arrays.asList("スイカ", "バナナ", "ブドウ", "ミカン", "イチゴ", "パイナップル", "リンゴ", 9 "トマト", "ピーチ", "かぼちゃ", "チェリー", "セブン", "スターバー", "ベル", "コイン", "おばけ")); 10 ArrayList<String> fruitsPictorialPatternPic = new ArrayList<String>(); 11 fruitsPictorialPatternPic = new ArrayList<>(Arrays.asList("????", "????", "????", "????", "????", "????", "????", "????", "????", 12 "????", "????", "➐", "★", "????", "????", "????")); 13 System.out.println("メダルを入れて下さい。<所有メダル数 30枚>"); 14 15 int numberOfMedalsOwned = 30; 16 System.out.println("Press Enter."); 17 Scanner scanner = new Scanner(System.in); 18 scanner.nextLine(); 19 int targetIndex01 = (int) (Math.random() * fruitsPictorialPatternPic.size()); 20 int targetIndex02 = (int) (Math.random() * fruitsPictorialPatternPic.size()); 21 int targetIndex03 = (int) (Math.random() * fruitsPictorialPatternPic.size()); 22 System.out.println(fruitsPictorialPatternPic.get(targetIndex01) + "" + "" + "" + "" + "|" 23 + fruitsPictorialPatternPic.get(targetIndex02) + "" + "" + "" + "" + "|" 24 + fruitsPictorialPatternPic.get(targetIndex03)); 25 int targetIndex04 = (int) (Math.random() * fruitsPictorialPatternPic.size()); 26 int targetIndex05 = (int) (Math.random() * fruitsPictorialPatternPic.size()); 27 int targetIndex06 = (int) (Math.random() * fruitsPictorialPatternPic.size()); 28 System.out.println(fruitsPictorialPatternPic.get(targetIndex04) + "" + "" + "" + "|" 29 + fruitsPictorialPatternPic.get(targetIndex05) + "" + "" + "" + "" + "|" 30 + fruitsPictorialPatternPic.get(targetIndex06)); 31 int targetIndex07 = (int) (Math.random() * fruitsPictorialPatternPic.size()); 32 int targetIndex08 = (int) (Math.random() * fruitsPictorialPatternPic.size()); 33 int targetIndex09 = (int) (Math.random() * fruitsPictorialPatternPic.size()); 34 System.out.println(fruitsPictorialPatternPic.get(targetIndex07) + "" + "" + "" + "" + "|" 35 + fruitsPictorialPatternPic.get(targetIndex08) + "" + "" + "" + "" + "|" 36 + fruitsPictorialPatternPic.get(targetIndex09)); 37 if (targetIndex04 == (int)(fruitsPictorialPatternString.size(15)) 38 && targetIndex05 == (int)(fruitsPictorialPatternString.size(15)) 39 && targetIndex06 == (int)(fruitsPictorialPatternString.size(15))) { 40 System.out.println("おばけが3つ並びました。10枚出てきました。"); 41 numberOfMedalsOwned += 10; 42 System.out.println("<所有メダル数" + numberOfMedalsOwned + "枚>"); 43 } else { 44 System.out.println("外れです。"); 45 numberOfMedalsOwned --; 46 System.out.println("<所有メダル数" + numberOfMedalsOwned + "枚>"); 47 } 48 } 49} 50
if分でエラーが出てしまいました。
if (targetIndex04 == (int)(fruitsPictorialPatternString.size(15))
&& targetIndex05 == (int)(fruitsPictorialPatternString.size(15))
&& targetIndex06 == (int)(fruitsPictorialPatternString.size(15)))
sizeをgetに変更して試してみたりしたのですが、エラーがきえません。
型の不一致でのエラーなのですが、解決ができないのですが、
int targetIndex07 = (int) (Math.random() * fruitsPictorialPatternPic.size());はエラーは起きないのに、
if (targetIndex04 == (int)(fruitsPictorialPatternString.size(15))
&& targetIndex05 == (int)(fruitsPictorialPatternString.size(15))
&& targetIndex06 == (int)(fruitsPictorialPatternString.size(15)))
はエラーが起きてしまう違いがよくわかりません。ご教授お願いしたいです。
追記 エラー内容
この行に複数マーカーがあります
- 行ブレークポイント:main [行: 38] - main(String[])
- 型 ArrayList<String> のメソッド size() は引数 (int) に適用で
きません
追記
試したこと
fruitsPictorialPatternPicの15番目のおばけの値を指定したいので、setではなくgetで試したがエラーが出てしまいます。
fruitsPictorialPatternPicの15番目が3つそろった場合の条件式を完成させたいです
追記 get
この行に複数マーカーがあります
- 行ブレークポイント:main [行: 38] - main
(String[])
- String から int へキャストすることはできま
せん
java
1import java.util.ArrayList; 2import java.util.Arrays; 3import java.util.Scanner; 4 5public class main { 6 public static void main(String[] args) { 7 ArrayList<String> fruitsPictorialPatternString = new ArrayList<String>(); 8 fruitsPictorialPatternString = new ArrayList<>(Arrays.asList("スイカ", "バナナ", "ブドウ", "ミカン", "イチゴ", "パイナップル", "リンゴ", 9 "トマト", "ピーチ", "かぼちゃ", "チェリー", "セブン", "スターバー", "ベル", "コイン", "おばけ")); 10 ArrayList<String> fruitsPictorialPatternPic = new ArrayList<String>(); 11 fruitsPictorialPatternPic = new ArrayList<>(Arrays.asList("????", "????", "????", "????", "????", "????", "????", "????", "????", 12 "????", "????", "➐", "★", "????", "????", "????")); 13 System.out.println("メダルを入れて下さい。<所有メダル数 30枚>"); 14 15 int numberOfMedalsOwned = 30; 16 System.out.println("Press Enter."); 17 Scanner scanner = new Scanner(System.in); 18 scanner.nextLine(); 19 int targetIndex01 = (int) (Math.random() * fruitsPictorialPatternPic.size()); 20 int targetIndex02 = (int) (Math.random() * fruitsPictorialPatternPic.size()); 21 int targetIndex03 = (int) (Math.random() * fruitsPictorialPatternPic.size()); 22 System.out.println(fruitsPictorialPatternPic.get(targetIndex01) + "" + "" + "" + "" + "|" 23 + fruitsPictorialPatternPic.get(targetIndex02) + "" + "" + "" + "" + "|" 24 + fruitsPictorialPatternPic.get(targetIndex03)); 25 int targetIndex04 = (int) (Math.random() * fruitsPictorialPatternPic.size()); 26 int targetIndex05 = (int) (Math.random() * fruitsPictorialPatternPic.size()); 27 int targetIndex06 = (int) (Math.random() * fruitsPictorialPatternPic.size()); 28 System.out.println(fruitsPictorialPatternPic.get(targetIndex04) + "" + "" + "" + "|" 29 + fruitsPictorialPatternPic.get(targetIndex05) + "" + "" + "" + "" + "|" 30 + fruitsPictorialPatternPic.get(targetIndex06)); 31 int targetIndex07 = (int) (Math.random() * fruitsPictorialPatternPic.size()); 32 int targetIndex08 = (int) (Math.random() * fruitsPictorialPatternPic.size()); 33 int targetIndex09 = (int) (Math.random() * fruitsPictorialPatternPic.size()); 34 System.out.println(fruitsPictorialPatternPic.get(targetIndex07) + "" + "" + "" + "" + "|" 35 + fruitsPictorialPatternPic.get(targetIndex08) + "" + "" + "" + "" + "|" 36 + fruitsPictorialPatternPic.get(targetIndex09)); 37 if (targetIndex04 == (int)(fruitsPictorialPatternString.get(15)) 38 && targetIndex05 == (int)(fruitsPictorialPatternString.get(15)) 39 && targetIndex06 == (int)(fruitsPictorialPatternString.get(15))) { 40 System.out.println("おばけが3つ並びました。10枚出てきました。"); 41 numberOfMedalsOwned += 10; 42 System.out.println("<所有メダル数" + numberOfMedalsOwned + "枚>"); 43 } else { 44 System.out.println("外れです。"); 45 numberOfMedalsOwned --; 46 System.out.println("<所有メダル数" + numberOfMedalsOwned + "枚>"); 47 } 48 } 49} 50
回答3件
あなたの回答
tips
プレビュー