gpt4 book ai didi

java - 创建随机数计算器

转载 作者:行者123 更新时间:2023-12-02 04:20:54 25 4
gpt4 key购买 nike

请帮忙。我正在尝试创建一个数学学习系统,其中有 4 种不同数学运算的菜单。当选择一个操作时,它将生成一组执行该操作的数字。请帮我修复代码

import java.util.Scanner;
import java.util.Random;

public class advanceMath {
public static void main(String[]args) {


Random firstnum = new Random();
int number1 = firstnum.nextInt(9)+1;

Random secondnum = new Random();
int number2 = secondnum.nextInt(9);


Scanner input = new Scanner(System.in);

String dec;

int firstnum,secondnum; int total = 0 ;

System.out.println("choose letter ");
System.out.println("A.Addition");
System.out.println("B.SUBTRACTION");
System.out.println("C.MULTIPLICATION");
System.out.println("D.DIVISION");

System.out.println("letter i choose");
dec = input.next();

if(dec.equalsIgnoreCase("a")){

System.out.println(number1);
firstnum = input.nextInt();

System.out.println(number2);
secondnum = input.nextInt();

total = number1 + number2;

System.out.println("Total = "+ total);
System.out.println("Congratulation"); }


if(dec.equalsIgnoreCase("b")){
System.out.println(number1);
firstnum = input.nextInt();

System.out.println(number2);
secondnum = input.nextInt();

total = number1 - number2;

System.out.println("Total = "+ total);
System.out.println("Congratulation"); }

if(dec.equalsIgnoreCase("c")){
System.out.println(number1);
firstnum = input.nextInt();

System.out.println(number2);
secondnum = input.nextInt();

total = number1 * secondnum;

System.out.println("Total = "+ total);
System.out.println("Congratulation"); }

if(dec.equalsIgnoreCase("d")){
System.out.println(number1);
firstnum = input.nextInt();

System.out.println(number2);
secondnum = input.nextInt();

total = firstnum / secondnum;

System.out.println("Total = "+ total);
System.out.println("Congratulation"); }

else System.exit(0);
}

}

最佳答案

您声明了 firstnumsecondnum 两次

Random firstnum = new Random();

Random secondnum = new Random();

int firstnum,secondnum; int total = 0 ;

删除/重命名其中之一。

关于java - 创建随机数计算器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32778471/

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