ログインしてさらにmixiを楽しもう

コメントを投稿して情報交換!
更新通知を受け取って、最新情報をゲット!

Java質問&情報提供サイトコミュのException try〜catchについて。

  • mixiチェック
  • このエントリーをはてなブックマークに追加
お世話になります…
相も変わらず今日が期限の課題にてこずっています…
説明を読んでも、Exceptionと、try〜catchについて全く理解できずでもう頭が痛いです…
もしよろしかったら、この問題の解き方を教えていただけないでしょうか…
よろしくお願いします!!
1.If your program crashes, 50 points will be deducted, as the goal of this assignment is to learn how to use try-catch blocks.

2.Write a Java program that implements this algorithm:
a.Display a calculator menu

b.Ask the user to enter the integer choice, followed by two (2) integers

c.Get the integer input by using class Scanner method nextInt() & store the 3 integers in 3 variables

d.Use else-if statements or a switch statement to calculate the result of these four integer functions (division, modulus, gcd, lcm) using 4 static methods. Note that division and modulus will automatically throw an ArithmeticException exception, if the user enters 0 as the third integer. The methods for GCD & LCM need to be designed so that both throw a NegativeIntegerException exception, if the user enters a negative number for the second or third integer.

e.Each method must have two integer parameters and return an integer result. For example, you should use this method for the division operator:
public static int division(int x, int y){
int result = x / y;
return result;
}

f.For the greatest common divisor (GCD), use this algorithm:
while(y != 0){
temporary = y
y = x % temporary
x = temporary
}
gcd = x

g.For the lowest common multiple (LCM), use this formula:
(x*y)/(GCD of x & y)

h.Display the result of integer (whole number) operators division, modulus, gcd, lcm

i.If the user enters any incorrect input, the program should give an appropriate error message, and then display the menu again. Use try-catch blocks to do this. (To check the integer range for selecting a choice from the menu, use an if statement, not a try-catch block.)

***** CALCULATOR MENU *****
1. Division
2. Modulus
3. Greatest Common Divisor
4. Lowest Common Multiple
5. Exit the program
Enter your choice followed by two integers: 1 18 12
18 / 12 = 1

***** CALCULATOR MENU *****
1. Division
2. Modulus
3. Greatest Common Divisor
4. Lowest Common Multiple
5. Exit the program
Enter your choice followed by two integers: 2 18 12
18 % 12 = 6

***** CALCULATOR MENU *****
1. Division
2. Modulus
3. Greatest Common Divisor
4. Lowest Common Multiple
5. Exit the program
Enter your choice followed by two integers: 3 18 12
GCD of 18 & 12 = 6

***** CALCULATOR MENU *****
1. Division
2. Modulus
3. Greatest Common Divisor
4. Lowest Common Multiple
5. Exit the program
Enter your choice followed by two integers: 4 18 12
LCM of 18 & 12 = 36

***** CALCULATOR MENU *****
1. Division
2. Modulus
3. Greatest Common Divisor
4. Lowest Common Multiple
5. Exit the program
Enter your choice followed by two integers: 1 1 0
java.lang.ArithmeticException: / by zero

***** CALCULATOR MENU *****
1. Division
2. Modulus
3. Greatest Common Divisor
4. Lowest Common Multiple
5. Exit the program
Enter your choice followed by two integers: 2 2 0
java.lang.ArithmeticException: / by zero

***** CALCULATOR MENU *****
1. Division
2. Modulus
3. Greatest Common Divisor
4. Lowest Common Multiple
5. Exit the program
Enter your choice followed by two integers: 4 0 0
java.lang.ArithmeticException: / by zero

***** CALCULATOR MENU *****
1. Division
2. Modulus
3. Greatest Common Divisor
4. Lowest Common Multiple
5. Exit the program
Enter your choice followed by two integers: 3 -3 3
NegativeIntegerException: in greatest common divisor method

***** CALCULATOR MENU *****
1. Division
2. Modulus
3. Greatest Common Divisor
4. Lowest Common Multiple
5. Exit the program
Enter your choice followed by two integers: 3 3 -3
NegativeIntegerException: in greatest common divisor method

***** CALCULATOR MENU *****
1. Division
2. Modulus
3. Greatest Common Divisor
4. Lowest Common Multiple
5. Exit the program
Enter your choice followed by two integers: 3 -3 -3
NegativeIntegerException: in greatest common divisor method

***** CALCULATOR MENU *****
1. Division
2. Modulus
3. Greatest Common Divisor
4. Lowest Common Multiple
5. Exit the program
Enter your choice followed by two integers: 4 -4 4
NegativeIntegerException: in least common multiple method

***** CALCULATOR MENU *****
1. Division
2. Modulus
3. Greatest Common Divisor
4. Lowest Common Multiple
5. Exit the program
Enter your choice followed by two integers: 4 4 -4
NegativeIntegerException: in least common multiple method

***** CALCULATOR MENU *****
1. Division
2. Modulus
3. Greatest Common Divisor
4. Lowest Common Multiple
5. Exit the program
Enter your choice followed by two integers: 4 -4 -4
NegativeIntegerException: in least common multiple method

***** CALCULATOR MENU *****
1. Division
2. Modulus
3. Greatest Common Divisor
4. Lowest Common Multiple
5. Exit the program
Enter your choice followed by two integers: 1 2 spam
java.util.InputMismatchException: Please enter 3 integers

***** CALCULATOR MENU *****
1. Division
2. Modulus
3. Greatest Common Divisor
4. Lowest Common Multiple
5. Exit the program
Enter your choice followed by two integers: a b c
java.util.InputMismatchException: Please enter 3 integers

***** CALCULATOR MENU *****
1. Division
2. Modulus
3. Greatest Common Divisor
4. Lowest Common Multiple
5. Exit the program
Enter your choice followed by two integers: this is a pen
java.util.InputMismatchException: Please enter 3 integers

***** CALCULATOR MENU *****
1. Division
2. Modulus
3. Greatest Common Divisor
4. Lowest Common Multiple
5. Exit the program

Enter your choice followed by two integers: 1 w 3
java.util.InputMismatchException: Please enter 3 integers

***** CALCULATOR MENU *****
1. Division
2. Modulus
3. Greatest Common Divisor
4. Lowest Common Multiple
5. Exit the program
Enter your choice followed by two integers: -1 2 3
INCORRECT INPUT: Please answer a choice from 1 to 5

***** CALCULATOR MENU *****
1. Division
2. Modulus
3. Greatest Common Divisor
4. Lowest Common Multiple
5. Exit the program
Enter your choice followed by two integers: 999 999 999
INCORRECT INPUT: Please answer a choice from 1 to 5

***** CALCULATOR MENU *****
1. Division
2. Modulus
3. Greatest Common Divisor
4. Lowest Common Multiple
5. Exit the program
Enter your choice followed by two integers: 5

コメント(10)

System.in か何かから入力を読み取り、Scannerクラスへ渡す。
入力が
整数 整数 整数
じゃないときはthrow InputMismatchException
半角スペースをセパレータとして3つに分離する。
nextInt()を呼び出す度にその整数を1個ずつ取得する。
それらはどこかへ保存しておく。

整数の引数を二つもつstaticなメソッドを4つ作成する。
それぞれは、除算・剰余・最大公約数・最小公倍数を計算する。
除算・剰余メソッドは、分母が0ではいけないので
そのときは throw Arithmetic Exception
最大公約数・最小公倍数を計算するメソッドでは
二つの引数両方が正でない場合、
throw NegativeIntegerException
(0はいいんだろか・・・)

Scannerクラスで得られた1番目の整数が
1なら、
 割り算メソッドに2番目3番目の引数を渡す
2なら、

(略)

最後に、計算結果を出力する。

Exceptionを投げ得るメソッド呼び出しをtryで囲み、
それに従うcatchブロックで例外をキャッチしてエラーメッセージを出力する。
処理が終わったら、再度入力待ち状態にする。
Exeption、try、catchの何がわからないんでしょうか? 納得のいかない部分をもう少し詳しく書いてもらえると答えやすくなると思います。


例えば、なんでこんなものが必要なのかわからない、だとか、catchした後何をすればいいのかわからかい、とか。
書いちゃったので載せます。なるべく読まずに御自分でやられた方がよいと思います。どうしても提出に間に合わなかったときの最終兵器ということで・・・。
一応動作はチェックしていますが、提出する場合には御自分でも確認してください。

import java.io.*;
import java.util.*;

class ExceptionTest{
public static void main(String[] args){
int ope = 0;
do{
try{
ExceptionTest.display();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String code = br.readLine();
ExceptionTest.check(code);

Scanner scanner = new Scanner(code);
ope = scanner.nextInt();
int a = scanner.nextInt();
int b = scanner.nextInt();

if(ope == 1){
ExceptionTest.division(a,b);
}
else if(ope == 2){
ExceptionTest.modulus(a,b);
}
else if(ope == 3){
ExceptionTest.gcd(a,b);
}
else if(ope == 4){
ExceptionTest.lcm(a,b);
}
else if(ope != 5){
System.out.println("unknown error in this program.");
}
}
catch(Exception e){
System.out.println(e.getMessage());
}
}while(ope != 5);
}

public static void display(){
System.out.println();
System.out.println("***** CALCULATOR MENU *****");
System.out.println("1.Division");
System.out.println("2.Modulus");
System.out.println("3.Greatest Common Divisor");
System.out.println("4.Lowest Common Multiple");
System.out.println("5.Exit the program");
System.out.print("Enter your choice followed by two integers: ");
}

public static void check(String code) throws InputMismatchException{
if(!code.matches(" *-?[0-9]+ +-?[0-9]+ +-?[0-9]+ *")){
throw new InputMismatchException("Please enter 3 integers.");
}
if(!code.matches(" *[1-5]+ +-?[0-9]+ +-?[0-9]+ *")){
throw new InputMismatchException("Select operator from 1 to 5.");
}
}

public static void division(int a , int b){
System.out.println(a+" / "+b+" = "+a/b);
}

public static void modulus(int a , int b){
System.out.println(a+" % "+b+" = "+a%b);
}

public static void gcd(int a , int b) throws NegativeIntegerException{
if(a < 0 || b < 0){
throw new NegativeIntegerException("in greatest common divisor method.");
}
System.out.println("GCD of "+a+" & "+b+" = "+gcdCal(a,b));
}

public static int gcdCal(int x , int y){
while(y!=0){
int temp = y;
y = x%temp;
x = temp;
}
return x;
}

public static void lcm(int a , int b) throws NegativeIntegerException{
if(a < 0 || b < 0){
throw new NegativeIntegerException("in lowest common multiple method.");
}
System.out.println("LCM of "+a+" & "+b+" = "+a*b/gcdCal(a,b));
}
}

class NegativeIntegerException extends Exception{
NegativeIntegerException(String str){
super(str);
}
}
みなさんどうもありがとうございます><
ところでメニューで5を選ぶ際に、
5だけであとの2つの数をいれなくても終了できる
ようにするにはどうすればよかったのでしょうか…
鋭いところを突いてきますね・・・。

if(code.matches(" *5.*")){
continue;
}



ExceptionTest.check(code);

の前に入れればうまくいくと思います。未確認です。
rayさん、返信ありがとうございます。
教えていただいた通りに入れてみたのですが、、
またメニューが出てきてしまって終了できず…
お手数かけて申し訳ないのですが、、
どうすればいいかまた教えていただけないでしょうか…
すいません。続行ではなくて終了でしたね。
上記コメントの continue; を break; に変えてください。
肝心のtry〜catchの意味はわかったんでしょうか。それとも中身の理解よりも課題提出が最優先事項?
rayさん、いつもありがとうございます。
おかげさまで謎が解けました…。感謝です!!

いろはさん、ごめんなさい…
提出締切りはもう過ぎいてたのですが…気になって…
でも自分ではどうにもできなくて…甘えてしまいました。。
もうJava自体、全然理解できずで途方に暮れるばかりで…
私には向いてないのかもしれませんね…

ありがとうございました!!

ログインすると、みんなのコメントがもっと見れるよ

mixiユーザー
ログインしてコメントしよう!

Java質問&情報提供サイト 更新情報

Java質問&情報提供サイトのメンバーはこんなコミュニティにも参加しています

星印の数は、共通して参加しているメンバーが多いほど増えます。

人気コミュニティランキング