构建一个简单的程序,使用方法,静态变量和方法以及最终变量来解决附加问题。我已经使用下面的代码启动了该程序,但我遇到了4个错误。我已经包含了与我的.java文件相关的所有错误。
目标是尝试将输出的2个数字添加到一起并告诉用户是真还是假。
命令提示符错误:
CAI.java:43: error: '.class' expected
int max = 50; int min = 1;
^
CAI.java:43: error: illegal start of expression
int max = 50; int min = 1;
^
CAI.java:43: error: ';' expected
int max = 50; int min = 1;
^
CAI.java:44: error: not a statement
rand.nextInt((max - min) + 1) + min;
^
4 errors
CAI.java
import java.util.Scanner;
import java.util.Random;
public class CAI
{
static Scanner input = new Scanner(System.in);
static int addition;
//main method
public static void main(String[] arguments)
{
//declare variables
String name;
int addition;
int subtraction;
int multiplication;
//boolean = true;
//welcome the user
System.out.println("Welcome to Computer Assisted Instruction");
//get the user's name
System.out.print("Enter your name: ");
name = input.next();
System.out.printf("Hi %s. Let's practice addition!", name);
}
private static boolean addition(int num1, int num2)
{
return (num1+num2)
//generate double digit numbers positive and negative
int max = 50; int min = 1;
rand.nextInt((max - min) + 1) + min;
//Addition
System.out.printf("What is %d + %d? ", a,b);
addition = input.nextInt();
}
}
答案 0 :(得分:0)
return (num1+num2)
语句之后的所有代码都是不可访问的代码...
此外,该语句缺少分号,这是msg
的原因错误:'。class'预期
添加分号后,将出现无法访问的代码错误
改为:
int max = 50; int min = 1;
rand.nextInt((max - min) + 1) + min;
//Addition
System.out.printf("What is %d + %d? ", a,b);
addition = input.nextInt();
return (num1+num2);
编辑:
如果声明方法返回布尔值,则无法返回anoter类型...
您的方法必须声明为:
private static int addition(int num1, int num2)
答案 1 :(得分:0)
总结你做错了什么:
return
然后编写你的方法逻辑,这不起作用;
return ...
rand
未定义或类型错误a
和b
未定义rand.nextInt
的结果未分配给任何内容addition
有两个参数,但您可以在没有任何修复所有这些最终结果如下:
private static boolean addition() {
Random rand = new Random();
int max = 50; int min = 1;
int a = rand.nextInt((max - min) + 1) + min;
int b = rand.nextInt((max - min) + 1) + min;
System.out.printf("What is %d + %d? ", a, b);
int addition = input.nextInt();
return (a + b) == addition;
}