英文:
How can I fix a java code that has no class
问题
import java.util.*;
public class Lab21_Vars {
public static void main(String[] args) {
int i, j;
int var0, var1;
// Fix 1: Correctly typecast 5.0 to an int so it can be assigned to var3.
int var2 = 0, var3 = (int) 5.0;
//int var2 = 0, var3 = 5.0;
// Fix 2: Correctly declare a variable as an array.
int[] arri0 = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
//int arri0 = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
double[] arrd0 = {0.0, 1.0, 2.0, 3.0, 4.0};
// Fix 3: What's wrong with temp? Fix it.
int temp = 0;
if (i < j) {
//int temp = 0;
System.out.println("Temp is " + temp);
} else {
temp = 1;
System.out.println("Temp is " + temp);
}
// Fix 4: Correctly calculate the sum of squares and print it as total.
int total = 0;
for (int i = 0; i < 10; i++) {
//int total = 0;
total += i * i;
}
System.out.println("i value is " + i);
System.out.println("Total is " + total);
// Fix 5: Figure out the logical error and correct it.
Cheese jack;
Cheese monterey = new Cheese("Monterey");
jack = monterey;
System.out.println("Monterey name is " + monterey.getName());
jack.setName("Jack");
System.out.println("Jack name is " + jack.getName());
System.out.println("Monterey name is still " + monterey.getName());
// Fix 6: Make the following code shorter by combining redundant
// (unnecessary/duplicate) statements
Scanner input = new Scanner(System.in);
System.out.print("Enter first number: ");
int num1 = input.nextInt();
System.out.print("Enter second number: ");
int num2 = input.nextInt();
if (num1 > var3)
System.out.println("First is greater");
else
System.out.println("First is Less than or equal");
if (num2 < var3)
System.out.println("Second is Less than");
else
System.out.println("Second is Greater or equal");
/*
if (input.nextInt() > var3) {
System.out.print("Enter second number: ");
int num2 = input.nextInt();
System.out.println("First is greater");
if (num2 < var3)
System.out.println("Second is Less than");
else
System.out.println("Second is Greater or equal");
} else {
System.out.print("Enter second number: ");
int num2 = input.nextInt();
System.out.println("First is Less than or equal");
if (num2 < var3)
System.out.println("Second is Less than");
else
System.out.println("Second is Greater or equal");
}
*/
// Fix 7: Print out the first number entered by the user
// (Hint - You will need to modify the above code)
System.out.println("The first number was " + num1);
}
}
英文:
I am trying to correct the error in fix 5 below. It asks for the logical error and to correct it. I think the logical error is that there is no cheese class, but how would I edit the code so that the logical error is fix? I am not sure how to add the class needed in the code and if its needed though?
import java.util.*;
public class Lab21_Vars {
public static void main(String[] args) {
int i, j;
int var0, var1;
// Fix 1: Correctly typecast 5.0 to an int so it can assigned to var3.
int var2 = 0, var3 = (int) 5.0;
//int var2 = 0, var3 = 5.0;
// Fix 2: Correctly declare a variable as an array.
int [] arri0 = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
//int arri0 = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
double[] arrd0 = {0.0, 1.0, 2.0, 3.0, 4.0};
// Fix 3: What's wrong with temp? Fix it.
int temp = 0;
if (i < j) {
//int temp = 0;
System.out.println("Temp is " + temp);
} else {
temp = 1;
System.out.println("Temp is " + temp);
}
// Fix 4: Correctly calculate square of sums and print it as total.
int total = 0;
for (int i = 0; i < 10; i++) {
//int total = 0;
total += i*i;
}
System.out.println("i value is " + i);
System.out.println("Total is " + total);
// Fix 5: Figure out the logical error and correct it.
Cheese jack;
Cheese monterey = new Cheese("Monterey");
jack = monterey;
System.out.println("Monterey name is " + monterey.getName());
jack.setName("Jack");
System.out.println("Jack name is " + jack.getName());
System.out.println("Monterey name is still " + monterey.getName());
// Fix 6: Make the following code shorter by combining redundant
// (unnecessary/duplicate) statements
Scanner input = new Scanner(System.in);
System.out.print("Enter first number: ");
int num1 = input.nextInt();
System.out.print("Enter second number: ");
int num2 = input.nextInt();
if (num1 > var3)
System.out.println("First is greater");
else
System.out.println("First is Less than or equal");
if (num2 < var3)
System.out.println("Second is Less than");
else
System.out.println("Second is Greater or equal");
/*
if (input.nextInt() > var3) {
System.out.print("Enter second number: ");
int num2 = input.nextInt();
System.out.println("First is greater");
if (num2 < var3)
System.out.println("Second is Less than");
else
System.out.println("Second is Greater or equal");
} else {
System.out.print("Enter second number: ");
int num2 = input.nextInt();
System.out.println("First is Less than or equal");
if (num2 < var3)
System.out.println("Second is Less than");
else
System.out.println("Second is Greater or equal");
}
*/
// Fix 7: Print out the first number entered by the user
// (Hint - You will need to modify the above code)
System.out.println("The first number was " + num1);
}
}
答案1
得分: 1
Cheese jack;
Cheese monterey = new Cheese("Monterey");
jack = monterey;
System.out.println("Monterey name is " + monterey.getName());
jack.setName("Jack");
System.out.println("Jack name is " + jack.getName());
System.out.println("Monterey name is still " + monterey.getName());
不,这里并不是指Cheese
类的缺失导致了预期的逻辑错误。逻辑错误是指代码中的逻辑是否正确,即在运行时是否表现正确,而不是Cheese
未引用任何现有类而导致的编译错误(完全阻止你无法运行代码)。
因此,你需要假设存在一个Cheese
类,并且奶酪有一个可以获取和设置的名称。
该代码似乎假设最后一条语句会打印出
> Monterey name is still Monterey
然而,使用setName()
和getName()
的标准行为,情况并非如此。因此,你的任务是找出为什么会打印出其他内容,并可能找出如何修复代码以获得上述输出。
我很高兴并且有信心将这部分工作交给你。
英文:
Cheese jack;
Cheese monterey = new Cheese("Monterey");
jack = monterey;
System.out.println("Monterey name is " + monterey.getName());
jack.setName("Jack");
System.out.println("Jack name is " + jack.getName());
System.out.println("Monterey name is still " + monterey.getName());
No, the absence of the Cheese
class is hardly the intended logical error here. A logical error would refer to the logic in this code, whether it behaves correctly when run, not to a compile error from Cheese
not referring to any existing class (preventing you from running the code at all).
So you will need to assume that there is a Cheese
class and that a cheese has a name that you can get and set.
The code seems to assume that the last statement will print
> Monterey name is still Monterey
With a standard behaviour of setName()
and getName()
this will however not be the case. So your job is to find out why it prints something else and presumably how to fix the code to get the above output.
I am happily and confidently leaving that to you.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论