如何在 while 循环外部使用变量 “rev”?

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

How to use the variable "rev" outside the while loop?

问题

运行下面显示的代码后,我收到以下错误:

rev cannot be resolved to a variable

我已经尝试在while循环外部声明变量,但只会导致更多错误。以下是代码:

import java.util.Scanner;

public class PalindromeProject {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner num = new Scanner(System.in);
        System.out.println("Enter number to check if palindrome");
        int number = num.nextInt();
        int temp = number;
        int rev = 0;  // 添加此行来声明变量
        while (temp != 0) {
            int rem = temp % 10;
            rev = (rev * 10) + rem;
            temp = temp / 10;
        }
        if (rev == number) {
            System.out.println("Number provided is palindrome");
        } else {
            System.out.println("Number provided is not palindrome");
        }
    }
}

在代码中添加了 int rev = 0; 来声明 rev 变量。

英文:

After running the code shown below, I receive the following error:

rev cannot be resolved to a variable

I have tried to declare the variable outside the while loop, but it only leads to more errors. Here is the code:

import java.util.Scanner;

public class PalindromeProject {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner num = new Scanner(System.in);
		System.out.println("Enter number to check if palindrome");
		int number = num.nextInt();
        int temp=number;
        while(temp!=0) {
         int rem=temp%10;
         int rev=(rev*10)+rem;
        	temp=temp/10;
        }
		if(rev==number){
        	System.out.println("Number provided is palindrome");
        }else {
        	System.out.println("Number provided is not palindrome");
        }
	}
}

答案1

得分: 0

以下是翻译好的内容:

无法将 rev 解析为变量的原因是因为您在其作用域之外执行了与它相关的操作。

您在 while 循环中创建了整数 rev。在这种情况下,您只能在循环内进行修改。

如果您想对 rev 执行操作,您必须在循环外初始化它。

代码应如下所示:

import java.util.Scanner;

public class PalindromeProject {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner num = new Scanner(System.in);
        System.out.println("Enter number to check if palindrome");
        int number = num.nextInt();
        int temp=number;
        int rev=0;
        while(temp!=0) {
            int rem=temp%10;
            rev=(rev*10)+rem;
            temp=temp/10;
        }
        if(rev==number){
            System.out.println("Number provided is palindrome");
        }else {
            System.out.println("Number provided is not palindrome");
        }
    }
}
英文:

The reason the rev cannot be resolved to a variable is because you are performing operations with it outside of its scope.

You created the integer rev in the while loop. In this case, you can only make modifications in the loop.

If you want to perform operations on rev, you have to initialize it outside of the loop.

It should look like the following:

import java.util.Scanner;

public class PalindromeProject {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner num = new Scanner(System.in);
        System.out.println("Enter number to check if palindrome");
        int number = num.nextInt();
        int temp=number;
        int rev=0;
        while(temp!=0) {
         int rem=temp%10;
         rev=(rev*10)+rem;
            temp=temp/10;
        }
        if(rev==number){
            System.out.println("Number provided is palindrome");
        }else {
            System.out.println("Number provided is not palindrome");
        }
    }
}

huangapple
  • 本文由 发表于 2020年9月19日 21:04:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/63969134.html
匿名

发表评论

匿名网友

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

确定