2016-04-03 13 views
0

私はちょっとした問題を抱えています。この割り当て(レッスン12 HW9)名前はNumberFormatExceptionについてはJava/Eclipse:例外ハンドラ - メッセージと変数を1つのPrintln行に出力

は、私が

「Calculator.javaの下に次のことをしなければならなかった、シンプルなコマンドライン計算機です。いずれかのオペランドが非数値である場合、プログラムが終了することに注意してください。非数値オペランドを処理する例外ハンドラを持つプログラムを作成します。プログラムは、終了する前に間違ったオペランドをユーザーに知らせるメッセージを表示する必要があります(下図参照)。

c:\exercise>java Exercise12_01 "4 + 5" 

4 + 5 = 9 

c:\exercise>java Exercise12_01 "4 - 5" 

4 - 5 = -1 

c:\exercise>java Exercise12_01 "4x - 5" 

Wrong Input: 4x 

SOコーディングのすべての数学の部分は、正しい終了し、動作しますが、私が把握するように見える傾けるだけの部分があるさ: は「終了する前に、誤ったオペランドのユーザーに通知メッセージを表示します」間違った入力:4x。私は、エラーメッセージだけでなく、エラー入力も表示するために何ができるかを知る必要があります。それはユーザーにエラーメッセージを表示する部分がここに

catch(InputMismatchException ex) 
    { 
     System.out.println("Bad input, please correct your operard."); 
    } //need to add the input error as well 

で、コードの末尾に完全なコードで、

import java.util.*; 
 
public class FillZerosOnes { 
 

 
public static void main(String[] args) { 
 
\t 
 
\t Scanner Read = new Scanner(System.in); 
 
\t Random randNum = new Random(); 
 
\t int n; 
 
\t int rowCount, columnCount; 
 
\t int maxrowCount, maxcolumnCount; 
 
\t ArrayList<Integer> Rows = new ArrayList(),Columns = new ArrayList(); 
 
\t int[][] matrix; 
 
\t 
 
\t System.out.println("Enter the array size n: "); 
 
\t n = Read.nextInt(); 
 
\t 
 
\t matrix = new int[n][n]; 
 
\t 
 
\t System.out.println("The random array size : "); 
 
\t 
 
\t 
 
\t for(int i = 0; i < n; i++) 
 
\t { 
 
\t 
 
\t \t for(int j = 0; j < n; j++) 
 
\t \t { 
 
\t matrix[i][j] = randNum.nextInt(2);//choose the number above 1 for the number you want to be less then 
 
\t //if I want 0 & 1, out 2 
 
\t 
 
\t System.out.print(matrix[i][j]); 
 
\t \t } 
 
\t \t System.out.println(); 
 

 
\t 
 
\t 
 
\t 
 
\t 
 
\t } 
 
\t 
 
\t columnCount = 0; 
 
\t maxrowCount= 0; 
 
\t maxcolumnCount =0; 
 
\t 
 
\t for (int i = 0; i < n; i++) 
 
\t { 
 
\t \t rowCount =0; 
 
\t \t for (int j = 0; j < n; j++) 
 
\t \t { 
 
\t \t \t if (matrix[i][j] ==1) 
 
\t \t \t { 
 
\t \t \t \t rowCount++; 
 
\t \t \t } 
 
\t \t } 
 
\t \t if (rowCount > maxrowCount) 
 
\t \t { 
 
\t \t \t maxrowCount = rowCount; 
 
\t \t \t Rows.removeAll(Rows); // or Rows = new Arraylist(); 
 
\t \t \t Rows.add(i); 
 
\t \t } 
 
\t \t else if (rowCount == maxrowCount) 
 
\t \t { 
 
\t \t \t Rows.add(i); 
 
\t \t } \t 
 
\t } 
 
\t //copy and paste 
 
\t for (int j = 0; j < n; j++) 
 
\t { 
 
\t \t columnCount =0; 
 
\t \t for (int i = 0; i < n; i++) 
 
\t \t { 
 
\t \t \t if (matrix[i][j] ==1) 
 
\t \t \t { 
 
\t \t \t \t columnCount++; 
 
\t \t \t } 
 
\t \t } 
 
\t \t if (columnCount > maxcolumnCount) 
 
\t \t { 
 
\t \t \t maxcolumnCount = columnCount; 
 
\t \t \t Columns.removeAll(Columns); // or Rows = new Arraylist(); 
 
\t \t \t Columns.add(j); 
 
\t \t } 
 
\t \t else if (columnCount == maxcolumnCount) 
 
\t \t { 
 
\t \t \t Columns.add(j); 
 
\t \t } \t 
 
\t } //end paste 
 
\t 
 
\t 
 
\t System.out.print("Largest row indices: "); 
 
\t for (int i = 0; i < Rows.size(); i++) 
 
\t { 
 
\t \t System.out.print(Rows.get(i)); 
 
\t } 
 
\t 
 

 
\t System.out.printf("\nLargest column indices: "); 
 
\t for (int i = 0; i < Columns.size(); i++) 
 
\t { 
 
\t \t System.out.print(Columns.get(i)); 
 
\t } 
 
} 
 
} \t

+0

これは –

答えて

0

のSystem.outありがとうございました。 println( "表示するテキスト" +変数);

+0

を無視してくださいで間違ったコードI OUTです問題は、ユーザーが「N」または「V」しか間違って取得する可能性があり、他の変数だけを残して間違っている変数のみを入れる方法を知らないということです。 –

0

私は通常、例外のスタックトレースログ:

System.out.println("Bad input, please correct your operand." + ex.getMessage()); 

をしかし、あなたはまた、同様にあなたの変数をプリントアウトすることができます

System.out.println("Bad input, please correct your operands: " + n + ", " + v); 
関連する問題