下記Javaのコードは、ユーザ入力にて長方形、三

Writer: admin Type: clarksre Date: 2019-01-05 00:00
下記Javaのコードは、ユーザ入力にて長方形、三角形、円を選択させ、選択した図形の面積を計算するプログラムです。61行目の「if (f==4) 」にて、ユーザが「4」を入力した際に、23行目の「 f = 4;」に戻り、再入力をお願いするdo_while文を書いたのですが、4を入力すると無限ループに陥ってしまいました。解決策があれば、具体的に、どこをどのように修正すれば良いか、ご回答お願いできますでしょうか。※また、下記コードでは「MathApp7」という別ファイルのclassを呼び出し実際の計算をしているのですが、文字数制限により「MathApp7」のコードまでは掲載できませんでした。import java.util.Scanner;import java.util.HashMap;class MathMaster7 { public static void main(String[] args) { System.out.print("How many shapes do you want to calcurate?: "); Scanner scannerNum = new Scanner(System.in); int num = scannerNum.nextInt(); HashMap<String, String> hashmap = new HashMap<String, String>(); hashmap.put("1", "rectangle"); hashmap.put("2", "triangle"); hashmap.put("3", "circle"); Scanner scanner = new Scanner(System.in); for (int i = 0; i < num; i++) { System.out.print("Input a number (1: rectangle, 2: triangle, 3: circle): "); String numStr = scanner.next(); int numInt = Integer.parseInt(numStr); numInt = 0; do { int f = 0; f = 4; switch (numInt) { case 1: MathApp7.Show(numStr); System.out.print("Type the length: "); Scanner scannerRec = new Scanner(System.in); int length = scannerRec.nextInt(); System.out.print("Type the width: "); int width = scannerRec.nextInt(); if (length == width) { System.out.println("That's a square, not a rectangle lol"); } else { System.out.println("Okay, let's continue on..."); } int rectangleArea = MathApp7.rectangleCalc(length, width); System.out.println(rectangleArea); break; case 2: MathApp7.Show(numStr); System.out.print("Type the length: "); Scanner scannerTri = new Scanner(System.in); int triLength = scannerTri.nextInt(); System.out.print("Type the width: "); int triWidth = scannerTri.nextInt(); int triangleArea = MathApp7.triangleCalc(triLength, triWidth); System.out.println(triangleArea); break; case 3: MathApp7.Show(numStr); System.out.print("Type the radius: "); Scanner scannerCir = new Scanner(System.in); double radius = scannerCir.nextDouble(); double circleArea = MathApp7.circleCalc(radius); System.out.println(circleArea); break; default: System.out.println("Please choose a shape from the selection!"); if (f==4) { System.out.println("Go back"); } break; } }while(numInt<=0|5<=numInt); } }}共感した0###元のソースを活かすならこうする。import java.util.HashMap;import java.util.Scanner;public class MathMaster7 { public static void main(String[] args) { System.out.print("How many shapes do you want to calcurate?: "); Scanner scannerNum = new Scanner(System.in); int num = scannerNum.nextInt(); HashMap<String, String> hashmap = new HashMap<String, String>(); hashmap.put("1", "rectangle"); hashmap.put("2", "triangle"); hashmap.put("3", "circle"); Scanner scanner = new Scanner(System.in); int numInt = 0; for (int i = 0; i < num; i++) { System.out.print("Input a number (1: rectangle, 2: triangle, 3: circle): "); do { String numStr = scanner.next(); numInt = Integer.parseInt(numStr); switch (numInt) { case 1: MathApp7.Show(numStr); System.out.print("Type the length: "); Scanner scannerRec = new Scanner(System.in); int length = scannerRec.nextInt(); System.out.print("Type the width: "); int width = scannerRec.nextInt(); if (length == width) { System.out.println("That's a square, not a rectangle lol"); } else { System.out.println("Okay, let's continue on..."); } int rectangleArea = MathApp7.rectangleCalc(length, width); System.out.println(rectangleArea); break; case 2: MathApp7.Show(numStr); System.out.print("Type the length: "); Scanner scannerTri = new Scanner(System.in); int triLength = scannerTri.nextInt(); System.out.print("Type the width: "); int triWidth = scannerTri.nextInt(); int triangleArea = MathApp7.triangleCalc(triLength, triWidth); System.out.println(triangleArea); break; case 3: MathApp7.Show(numStr); System.out.print("Type the radius: "); Scanner scannerCir = new Scanner(System.in); double radius = scannerCir.nextDouble(); double circleArea = MathApp7.circleCalc(radius); System.out.println(circleArea); break; default: System.out.println("Please choose a shape from the selection!"); if (numInt == 4) { System.out.println("Go back"); } break; } } while (numInt <= 0 | 5 <= numInt); } }}// giftfromtheworld q10201380225ナイス0
###もし自分で作るとしたら、こんな感じにするかな。/* MathMaster7.java ここから */import java.util.HashMap;import java.util.Map;import java.util.Scanner;public class MathMaster7 {private static Scanner scanner = new Scanner(System.in);private static Map<Integer, Shape> shapes = createShapeMap();public static void main(String[] args) {System.out.print("How many shapes do you want to calcurate?: ");int tryCnt = scanner.nextInt();for (int i = 0; i < tryCnt; i++) {routine();}}private static Map<Integer, Shape> createShapeMap() {Map<Integer, Shape> shapes = new HashMap<Integer, Shape>();shapes.put(1, new Rectangle());shapes.put(2, new Triangle());shapes.put(3, new Circle());return shapes;}/* MathMaster7.java 続く */
###ご丁寧にご対応いただきありがとうございました!

 

TAG