有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java为什么要将“body”重置为零?

我的程序绘制了一个浣熊怪物(一个身体和一个头部),身体必须在头部之前绘制。但当我运行程序时,它会说“不能添加身体部位!”当我输入2作为输入时(即使我已经绘制了主体)。注:1作为输入绘制主体,2绘制头部

我已经发现了这个问题,但我不知道如何解决它。问题是头部在进入“if(rollValue==2)…”之前被重置为0。我的程序可以绘制主体,但在它完成绘制主体后,我的“主体”变量似乎被设置回0。这就是为什么我的程序没有画头。关于如何修复它,使我的程序在绘制主体后不会将“主体”重置回0,有什么想法吗

import java.util.Scanner;

public class Cootie
{
   public static void main(String[] args)
   {
      boolean done = false;

      while (!done)
      {
         Scanner scanner = new Scanner(System.in);
         System.out.println("BODY PARTS:");
         System.out.println("1 = body");
         System.out.println("2 = head");
         System.out.println("3 = one leg");
         System.out.println("4 = one antenna");
         System.out.println("5 = one eye");
         System.out.println("6 = tail");
         System.out.println("What number did you roll?: ");
         int rollValue = scanner.nextInt();

         int body = 0;
         int head = 0;

         if (rollValue == 1)
         {
            if (body == 0)
            {
               body = 1;
            }
            else
            {
               System.out.println("Can't add body part!");
               System.out.println(" ");;
            }
         }
         else if (rollValue == 2)
         {
            if (body == 1 && head == 0)
            {
               head = 1;
            }
            else
            {
               System.out.println("Can't add body part!");
            }
         }
         else
         {
            System.out.println("Enter a valid input!");
         }
         if (body == 1)
         {
            System.out.println("------------------------------");
            System.out.println("You got the body!");
            System.out.println(" ");
            System.out.println(" ");
            System.out.println("   [ ]");
            System.out.println(" ");
            System.out.println("   [ ]");
            System.out.println(" ");
            System.out.println("   [ ]");
            System.out.println(" ");
         }
         if (head == 1)
         {
            System.out.println("You got the head!");
            System.out.println(" ");
            System.out.println("  (    )");
         }
         if (body == 1 && head == 1)
         {
            System.out.println("Congratulations you have completed your cootie!");
            done = true;
         }
      }
   }
}

共 (3) 个答案

  1. # 1 楼答案

    您已经在while循环中声明了body(和head)变量,因此将运行每个循环:

    int body = 0;
    int head = 0;
    

    所以每个循环,这些变量都被重置为0。要保留不同迭代的值,请在while循环之前声明并初始化它们,以便它们只初始化一次0。(也可以在循环开始之前声明并初始化Scanner。)

    int body = 0;
    int head = 0;
    Scanner scanner = new Scanner(System.in);
    
    while (!done)
    {
       // Other code still the same
    
  2. # 2 楼答案

    这是因为您声明body和head在while循环中。要解决此问题,请在

    while (!done)
    

    迭代

  3. # 3 楼答案

    您的body和head变量在while循环中初始化,这就是为什么每次都重置为零。只需将这些变量从while循环中移出

    当您希望在每次迭代中重置变量值时,或者如果您希望在循环中保持更新值,而不是在循环外初始化这些变量时,初始化循环内的变量