gpt4 book ai didi

java - 需要帮助使用 eclipse 制作算命程序

转载 作者:行者123 更新时间:2023-12-02 05:26:16 25 4
gpt4 key购买 nike

这个算命程序是基于小学生使用的折纸算命师。这个人必须选择算命先生里面显示的一个数字。所选择的数字在算命先生的打开和关闭中被算出来。然后,该人从算命师内部显示的可用数字中选择另一个数字(它们可能与之前的数字相同,也可能不同),并再次被算出。最终的数字已选定,翻盖下的财富已被读出!

Design and create a fortune telling program that works as follows:

Generate a number between 0 and 2 and allow the user to choose that number, that number plus one or that number plus 2 (in other words one of three consecutive numbers starting from the randomly chosen number) Generate another number (0-2) and based on that number display three colors (of a possible 4 colors) from which to choose based on the number and color combination selected, tell the user their fortune.

Some requirements: Choices must be random Color combination must also be random The first input must be a number, the second must be a color. Make sure to type these user variables appropriately. Type also has ramifications on the if /switch statement(s). Be careful!! Use switch statement(s) as appropriate (at least one must be used) There are six possible numbers and four possible colors = Make up 5 fortunes and reuse them Do NOT use arrays, lists, or functions/subroutines/methods yet

这就是我到目前为止所拥有的,但现在我陷入了困境。我需要添加一个 switch stmt 但我不知道如何添加。

import java.util.Scanner;
public class Lab3 {


public static void main(String[] args) {
// Program that generates a fortune depending on number and color the user picked

Scanner input = new Scanner(System.in);
String userResponse = "0,1,2";
//variables
String[] fortune = new String[5];
fortune[0]= "Something great is coming";
fortune[1]= "Lucky day today";
fortune[2]= "Be careful today";
fortune[3]= "You will get an A on your next test";
fortune[4]= "Someone special will be coming into your life shortly";

int randFortune;
final int MAX_WINGE = 3;

System.out.print("Pick a number 0-2:");

System.out.print("Choose a color(red, blue, yellow, green)");



randFortune = (int)(Math.random() * (MAX_WINGE)) + 1;



case 0:
System.out.println(fortune[0]);
break;
case 1:
System.out.println(fortune[1]);
break;
case 2:
System.out.println(fortune[2]);
break;
case 3:
System.out.println(fortune[3]);
break;
case 4:
System.out.println(fortune[4]);
break;



}
}

最佳答案

只需编写开关代码:

switch (randFortune) {
case 0:
System.out.println(fortune[0]);
break;
case 1:
// etc
}

引用switch keyword documentation以获得完整的解释。

<小时/>

附注看起来您不需要 switch,而只需要一行:

System.out.println(fortune[randFortune]);

关于java - 需要帮助使用 eclipse 制作算命程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25984238/

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