gpt4 book ai didi

Java 将 switch 语句转换为另一种类型

转载 作者:行者123 更新时间:2023-12-01 21:15:30 26 4
gpt4 key购买 nike

我对 Java 很陌生。我现在正在上我的第一堂 Java 课。

我正在尝试找到一种使用 switch 指令以外的东西重写以下代码的方法。此代码与数独游戏相关。我需要问这个问题几次,然后我需要根据答案计算统计数据。

请注意,我在初级类,无法使用高级 Java 代码。

String response = "Y";

while (response.toLowerCase().equals("y"))
{

System.out.println("Please choose the game level");
System.out.println(" -----------------------------------");
System.out.println(" Enter 1 for Sudoku level Beginner");
System.out.println(" Enter 2 for Sudoku level Advanced");
System.out.println(" Enter 3 for Sudoku level Expert");
System.out.println(" -----------------------------------");

Sudoku = sudoku.nextInt();

if (Sudoku == 1) {

int choice1 = 0;

nbGames_Easy++;

System.out.println("Have you won the game ?");

System.out.print(" Enter 4 for yes ");

System.out.print(" Enter 5 for no ");

choice1 = sudoku.nextInt();

sudoku.nextLine();

switch (choice1)

{

case 4:

nbGamesEasy_Finished++;

successRate_Easy = nbGamesEasy_Finished / nbGames_Easy * 100;

System.out.println("How many time did you take to fill the grid ?");

System.out.println("The time must be in minutes");

String display5 = sudoku.nextLine();

resolutionTime_easy = (nbGamesEasy_Finished * resolutionTime_easy) + Integer.parseInt(display5) / (nbGamesEasy_Finished);

break;

case 5:

successRate_Easy = nbGamesEasy_Finished - 1 / nbGames_Easy * 100;

default:

break;
}

}
}

最佳答案

对于初学者来说,您可以将 switch 语句替换为 if-else if 语句,就像您之前在代码中所做的那样。您可以尝试如下所示:

if (choice1 == 4){

nbGamesEasy_Finished++;

successRate_Easy = nbGamesEasy_Finished / nbGames_Easy * 100;

System.out.println("How many time did you take to fill the grid ?");

System.out.println("The time must be in minutes");

String display5 = sudoku.nextLine();

resolutionTime_easy = (nbGamesEasy_Finished * resolutionTime_easy) + Integer.parseInt(display5) / (nbGamesEasy_Finished);

}
else if (choice1 == 5){

successRate_Easy = nbGamesEasy_Finished - 1 / nbGames_Easy * 100;

}
else {

//default

}

关于Java 将 switch 语句转换为另一种类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40251879/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com