前提・実現したいこと
f(x)=ax^s+b型の素数を探すプログラムを作っています。
最初に、ソースコードPrimeを作りましたが、Prime2にして機能をつけたそうとしたら自分では対応できないコンパイルerrorがでてきました。
Prime2は、f(x)をある数(ここではstra[0])までで最も高い素数の生成割合を調べる機能を追加したいです。
発生している問題・エラーメッセージ
class,interfaceまたはenumがありません。(大量) 式の開始が不正です。(1つ)
該当のソースコード
以下がPrime2
java
1import java.io.*; 2 3public class Prime2{ 4 static int ss = 0; 5 static int[][] a = new int[50][20]; 6 static int[] data = new int[3]; 7 static String[] str10a; 8 static String[] stra; 9 static long drive = 1; 10 public static void main(String []args) throws IOException { 11 BufferedReader input = new BufferedReader (new InputStreamReader (System.in)); 12 System.out.println("初期設定を行います。"); 13 System.out.println("本ソフトでは主にan^s+bの計算を行います。"); 14 startSetting(); 15 calcu(); 16 } 17 public static void startSetting()throws IOException { 18 BufferedReader input = new BufferedReader (new InputStreamReader (System.in)); 19 System.out.println("a,s,bを入力してください。例えば3n^5+1にしたいならば、"); 20 System.out.println("3 5 1"); 21 System.out.println("と入力します。"); 22 String str10 = input.readLine( ); 23 System.out.println("数字指定ならば大文字A。\n全数指定ならば大文字B。\n素数探索ならば大文字C。\n素数含有量測定ならば大文字D。\n素数導出式探索ならば大文字Eを入力してください。"); 24 System.out.println("また、スペースを入力して、調査対象の数を入力してください。例えば17の全数指定は、"); 25 System.out.println("B 17"); 26 System.out.println("と入力します。"); 27 String str = input.readLine( ); 28 String[] wind = str.split(" "); 29 String[] wind2 = str10.split(" "); 30 str10a = new String[wind2.length]; 31 stra = new String[wind.length]; 32 for(int i = 0;i < stra.length;i++){ 33 stra[i] = wind[i]; 34 } 35 for(int i = 0;i < str10a.length;i++){ 36 str10a[i] = wind2[i]; 37 } 38 for(int anc = 1;anc <= stra.length - 1;anc++){ 39 a[ss][anc - 1] = Integer.parseInt(stra[anc]); 40 } 41 for(int anc = 0;anc < 3;anc++){ 42 data[anc] = Integer.parseInt(str10a[anc]); 43 } 44 } 45 public static void calcu(){ 46 if(stra[0].equals("A")){ 47 long s; 48 for(int i = 0;i < 20;i++){ 49 if(!(a[ss][i] == 0)){ 50 drive = 1; 51 for(int anc = 0;anc < data[1];anc++){ 52 drive = (drive * a[ss][i]); 53 } 54 s = drive * data[0] + data[2]; 55 if(isPrime(s)==true){ 56 System.out.println(a[ss][i] + "の計算結果:" + s + "素数"); 57 } 58 else{ 59 System.out.println(a[ss][i] + "の計算結果:" + s + "合成数"); 60 } 61 } 62 } 63 } 64 else if(stra[0].equals("B")){ 65 long grow; 66 for(int r = 1;r <= a[ss][0];r++){ 67 drive = 1; 68 for(int anc = 0;anc < data[1];anc++){ 69 drive = drive * r; 70 } 71 grow = drive * data[0] + data[2]; 72 if(isPrime(grow)==true){ 73 System.out.println(r + "の計算結果:" + grow + "素数"); 74 } 75 else{ 76 System.out.println(r + "の計算結果:" + grow + "合成数"); 77 } 78 } 79 } 80 else if(stra[0].equals("C")||stra[0].equals("D")||stra[0].equals("E")){ 81 if(stra[0].equals("C")||stra[0].equals("D")){ 82 syca(data[0],data[1],data[2]); 83 }else{ 84 double[][][] gman = new double[stra[0]][stra[0]][stra[0]]; 85 for(long dn = 1;stra[0] >= dn;dn++){ 86 for(long dn2 = 1;stra[0] >= dn2;dn2++){ 87 for(long dn3 = 1;stra[0] >= dn3;dn3++){ 88 gman[dn][dn2][dn3] = syca(dn,dn2,dn3); 89 } 90 } 91 } 92 double tta; 93 for(long dn = 0;gman.length > dn;dn++){ 94 for(long dn2 = 0;gman[dn].length > dn2;dn2++){ 95 for(long dn3 = 0;gman[dn][dn2].length > dn3;dn3++){ 96 if((dn == 0)&&(dn2 == 0)&&(dn3 == 0)){ 97 tta = gman[dn][dn2][dn3]; 98 }else{ 99 tta = Math.max(gman[dn][dn2][dn3],tta); 100 } 101 } 102 } 103 } 104 System.out.println(tta + "%"); 105 } 106 public static void syca(long nn1,long nn2,long nn3){ 107 long wq; 108 long ggh = 0; 109 double ggh2; 110 for(int gm = 1;gm <= a[ss][0];gm++){ 111 drive = 1; 112 for(int anc = 0;anc < nn2;anc++){ 113 drive = drive * gm; 114 } 115 wq = drive * nn1 + nn3; 116 if(isPrime(wq)==true){ 117 if(stra[0].equals("D")||stra[0].equals("E")){ 118 ggh++; 119 }else{ 120 System.out.println(gm + ":" + wq); 121 } 122 } 123 } 124 if(stra[0].equals("D")){ 125 ggh2 = ((double)ggh / (double)a[ss][0]) * 100; 126 System.out.println("素数含有量:" + ggh + "個"); 127 System.out.println("素数割合:" + ggh2 + "%"); 128 }else if(stra[0].equals("E")){ 129 ggh2 = ((double)ggh / (double)a[ss][0]) * 100; 130 return ggh2; 131 } 132 } 133 } 134 else{ 135 System.out.println("error AとB以外を入力している可能性"); 136 } 137 ss++; 138 try{ 139 repeat(); 140 } 141 catch(IOException e){ 142 System.out.println("error"); 143 } 144 } 145 } 146//以下Prime同様(省略)
以下Prime
java
1import java.io.*; 2 3public class Prime{ 4 static int ss = 0; 5 static int[][] a = new int[50][20]; 6 static int[] data = new int[3]; 7 static String[] str10a; 8 static String[] stra; 9 static long drive = 1; 10 public static void main(String []args) throws IOException { 11 BufferedReader input = new BufferedReader (new InputStreamReader (System.in)); 12 System.out.println("初期設定を行います。"); 13 System.out.println("本ソフトでは主にan^s+bの計算を行います。"); 14 startSetting(); 15 calcu(); 16 } 17 public static void startSetting()throws IOException { 18 BufferedReader input = new BufferedReader (new InputStreamReader (System.in)); 19 System.out.println("a,s,bを入力してください。例えば3n^5+1にしたいならば、"); 20 System.out.println("3 5 1"); 21 System.out.println("と入力します。"); 22 String str10 = input.readLine( ); 23 System.out.println("数字指定ならば大文字A。全数指定ならば大文字B。素数探索ならば大文字C。素数含有量測定ならば大文字Dを入力してください。"); 24 System.out.println("また、スペースを入力して、調査対象の数を入力してください。例えば17の全数指定は、"); 25 System.out.println("B 17"); 26 System.out.println("と入力します。"); 27 String str = input.readLine( ); 28 String[] wind = str.split(" "); 29 String[] wind2 = str10.split(" "); 30 str10a = new String[wind2.length]; 31 stra = new String[wind.length]; 32 for(int i = 0;i < stra.length;i++){ 33 stra[i] = wind[i]; 34 } 35 for(int i = 0;i < str10a.length;i++){ 36 str10a[i] = wind2[i]; 37 } 38 for(int anc = 1;anc <= stra.length - 1;anc++){ 39 a[ss][anc - 1] = Integer.parseInt(stra[anc]); 40 } 41 for(int anc = 0;anc < 3;anc++){ 42 data[anc] = Integer.parseInt(str10a[anc]); 43 } 44 } 45 public static void calcu(){ 46 if(stra[0].equals("A")){ 47 long s; 48 for(int i = 0;i < 20;i++){ 49 if(!(a[ss][i] == 0)){ 50 drive = 1; 51 for(int anc = 0;anc < data[1];anc++){ 52 drive = (drive * a[ss][i]); 53 } 54 s = drive * data[0] + data[2]; 55 if(isPrime(s)==true){ 56 System.out.println(a[ss][i] + "の計算結果:" + s + "素数"); 57 } 58 else{ 59 System.out.println(a[ss][i] + "の計算結果:" + s + "合成数"); 60 } 61 } 62 } 63 } 64 else if(stra[0].equals("B")){ 65 long grow; 66 for(int r = 1;r <= a[ss][0];r++){ 67 drive = 1; 68 for(int anc = 0;anc < data[1];anc++){ 69 drive = drive * r; 70 } 71 grow = drive * data[0] + data[2]; 72 if(isPrime(grow)==true){ 73 System.out.println(r + "の計算結果:" + grow + "素数"); 74 } 75 else{ 76 System.out.println(r + "の計算結果:" + grow + "合成数"); 77 } 78 } 79 } 80 else if(stra[0].equals("C")||stra[0].equals("D")){ 81 long wq; 82 long ggh = 0; 83 double ggh2; 84 for(int gm = 1;gm <= a[ss][0];gm++){ 85 drive = 1; 86 for(int anc = 0;anc < data[1];anc++){ 87 drive = drive * gm; 88 } 89 wq = drive * data[0] + data[2]; 90 if(isPrime(wq)==true){ 91 if(stra[0].equals("D")){ 92 ggh++; 93 }else{ 94 System.out.println(gm + ":" + wq); 95 } 96 } 97 } 98 if(stra[0].equals("D")){ 99 ggh2 = ((double)ggh / (double)a[ss][0]) * 100; 100 System.out.println("素数含有量:" + ggh + "個"); 101 System.out.println("素数割合:" + ggh2 + "%"); 102 } 103 } 104 else{ 105 System.out.println("error AとB以外を入力している可能性"); 106 } 107 ss++; 108 try{ 109 repeat(); 110 } 111 catch(IOException e){ 112 System.out.println("error"); 113 } 114 } 115 public static void repeat()throws IOException{ 116 try{ 117 BufferedReader input = new BufferedReader (new InputStreamReader (System.in)); 118 System.out.println("同じa,s,bでnを変えて実行するならば大文字A。(計算モード変更可能)"); 119 System.out.println("a,s,bを再設定するならば大文字B。"); 120 System.out.println("終了ならば大文字C。"); 121 String repeat = input.readLine( ); 122 if(repeat.equals("A")){ 123 System.out.println("数字指定ならば大文字A。全数指定ならば大文字B。素数探索ならば大文字Cを入力してください。"); 124 System.out.println("また、スペースを入力して、調査対象の数を入力してください。例えば17の全数指定は、"); 125 System.out.println("B 17"); 126 System.out.println("と入力します。"); 127 String str = input.readLine( ); 128 String[] wind = str.split(" "); 129 stra = new String[wind.length]; 130 for(int i = 0;i < stra.length;i++){ 131 stra[i] = wind[i]; 132 } 133 for(int anc = 1;anc <= stra.length - 1;anc++){ 134 a[ss][anc - 1] = Integer.parseInt(stra[anc]); 135 } 136 calcu(); 137 } 138 else if(repeat.equals("B")){ 139 startSetting(); 140 calcu(); 141 } 142 else if(repeat.equals("C")){ 143 return; 144 } 145 else{ 146 System.out.println("error"); 147 } 148 } 149 catch(IOException e){ 150 System.out.println("error"); 151 } 152 } 153 public static boolean isPrime(long num) { 154 if (num < 2) return false; 155 else if (num == 2) return true; 156 else if (num % 2 == 0) return false; 157 double sqrtNum = Math.sqrt(num); 158 for (int i = 3; i <= sqrtNum; i += 2){ 159 if (num % i == 0){ 160 return false; 161 } 162 } 163 return true; 164 } 165}
試したこと
括弧の対応はできるだけ探しました。
補足情報(FW/ツールのバージョンなど)
java 9.0.1
回答4件
あなたの回答
tips
プレビュー