如何修复一个没有类的Java代码。

huangapple go评论82阅读模式
英文:

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&#39;s wrong with temp? Fix it.
int temp = 0;
if (i &lt; j) {
//int temp = 0; 
System.out.println(&quot;Temp is &quot; + temp);
} else {
temp = 1;
System.out.println(&quot;Temp is &quot; + temp);
}
// Fix 4: Correctly calculate square of sums and print it as total.
int total = 0; 
for (int i = 0; i &lt; 10; i++) {
//int total = 0;
total += i*i;
}
System.out.println(&quot;i value is &quot; + i);
System.out.println(&quot;Total is &quot; + total);
// Fix 5: Figure out the logical error and correct it.
Cheese jack;
Cheese monterey = new Cheese(&quot;Monterey&quot;);
jack = monterey;
System.out.println(&quot;Monterey name is &quot; + monterey.getName());
jack.setName(&quot;Jack&quot;);
System.out.println(&quot;Jack name is &quot; + jack.getName());
System.out.println(&quot;Monterey name is still &quot; + monterey.getName());
// Fix 6: Make the following code shorter by combining redundant 
//        (unnecessary/duplicate) statements
Scanner input = new Scanner(System.in);
System.out.print(&quot;Enter first number: &quot;);
int num1 = input.nextInt();
System.out.print(&quot;Enter second number: &quot;);
int num2 = input.nextInt();
if (num1 &gt; var3) 
System.out.println(&quot;First is greater&quot;);
else
System.out.println(&quot;First is Less than or equal&quot;);
if (num2 &lt; var3)
System.out.println(&quot;Second is Less than&quot;);
else
System.out.println(&quot;Second is Greater or equal&quot;);
/*
if (input.nextInt() &gt; var3) {
System.out.print(&quot;Enter second number: &quot;);
int num2 = input.nextInt();
System.out.println(&quot;First is greater&quot;);
if (num2 &lt; var3)
System.out.println(&quot;Second is Less than&quot;);
else
System.out.println(&quot;Second is Greater or equal&quot;);
} else {
System.out.print(&quot;Enter second number: &quot;);
int num2 = input.nextInt();
System.out.println(&quot;First is Less than or equal&quot;);
if (num2 &lt; var3)
System.out.println(&quot;Second is Less than&quot;);
else
System.out.println(&quot;Second is Greater or equal&quot;);
}
*/
// Fix 7: Print out the first number entered by the user
//        (Hint - You will need to modify the above code)
System.out.println(&quot;The first number was &quot; + 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()的标准行为,情况并非如此。因此,你的任务是找出为什么会打印出其他内容,并可能找出如何修复代码以获得上述输出。

我很高兴并且有信心将这部分工作交给你。 如何修复一个没有类的Java代码。

英文:
Cheese jack;
Cheese monterey = new Cheese(&quot;Monterey&quot;);
jack = monterey;
System.out.println(&quot;Monterey name is &quot; + monterey.getName());
jack.setName(&quot;Jack&quot;);
System.out.println(&quot;Jack name is &quot; + jack.getName());
System.out.println(&quot;Monterey name is still &quot; + 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. 如何修复一个没有类的Java代码。

huangapple
  • 本文由 发表于 2020年4月5日 15:21:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/61039298.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定