该项目的目标是创建一个计算器,让用户选择要使用的操作,然后将其求解并添加到链接列表中。最后一个选择是查看历史记录,这些记录是过去的答案或解决方案。
我很新,这是一个我真的想独自完成的项目,但是我已经进行了研究,但似乎仍然不太了解如何解决。请向我解释,以便我更好地理解!还请评论我的代码的设计或流程。谢谢!
import java.util.Scanner;
import java.util.LinkedList;
public class projectOne {
public static void main(String[] args) {
int one = 0;
int two = 0;
int ans = 0;
int selection = 0;
Scanner scanner = new Scanner(System.in);
LinkedList l = new LinkedList();
do{
System.out.println("\nSelect an option: \n1. Addition \n2. Subtraction \n3. Multiplication \n4.Division \n5. See History");
selection = scanner.nextInt();
/* if (selection >= 1 && selection <= 4) {
System.out.println("\nEnter two numbers to be used in relevant calculation: ");
one = scanner.nextDouble();
two = scanner.nextDouble();
}
*/
switch(selection) {
case 1:
System.out.println("Performing Addition. \nEnter first number: ");
one = scanner.nextInt();
System.out.println("Enter second number: ");
two = scanner.nextInt();
System.out.println(one + " + " + two + " = " + (one + two));
ans = one + two;
l.append(ans);
break;
case 2:
System.out.println("Performing Subtraction. \nEnter first number: ");
one = scanner.nextInt();
System.out.println("Enter second number: ");
two = scanner.nextInt();
System.out.println(one + " - " + two + " = " + (one - two));
ans = one - two;
l.append(ans);
break;
case 3:
System.out.println("Performing Multiplication. \nEnter first number: ");
one = scanner.nextInt();
System.out.println("Enter second number: ");
two = scanner.nextInt();
System.out.println(one + " * " + two + " = " + (one * two));
ans = one * two;
l.append(ans);
break;
case 4:
System.out.println("Performing Division. \nEnter first number: ");
one = scanner.nextInt();
System.out.println("Enter second number: ");
two = scanner.nextInt();
System.out.println(one + " / " + two + " = " + (one / two));
ans = one / two;
l.append(ans);
break;
case 5:
System.out.println("History: \n" + l);
break;
}
} while(selection != 6);
}
}
答案 0 :(得分:2)
LinkedList
没有方法append
,可以使用add
,addLast
。
另外,请避免使用raw type
,而改为使用LinkedList<Integer>
。