Java中的计数器(非GUI)

时间:2015-10-17 19:42:19

标签: java

我正在尝试制作一个计数器,以便只要该人在循环期间继续响应是,它就会继续计数,然后在每个响应之后说明在这种情况下该人已经说了多少次是(y)。

以下是目前的代码:

/**
* Write a description of class dummygameclass here.
* 
* @author (your name) 
 * @version (a version number or a date)
 */

   import java.util.*;
          public class dummygameclass
    {
    // instance variables - replace the example below with your own
  public static void main(String [] args) throws InterruptedException{

   Scanner reader = new Scanner(System.in);

   int i1, i2, i3, i4, i5, i6, i7, i8, i9;
   String s1, s2, s3, s4, s5, s6, s7, s8, s9;
 boolean loop = true;
            while(loop=true){
   System.out.println("You walk up the stairs, but you shockingly see yourself back in the same room, same position, climb again?");
       s3 = reader.next();
       if(s3.equals("y")){
            System.out.println("You walk up the stairs, but you shockingly see yourself back in the same room, same position, climb again?");
       s3 = reader.next();
       int counter = 0;
       counter++;
       System.out.println(counter);

        }else if(s3.equals("n")){

            System.out.println("You have chosen not to climb the staircase.");
            loop=false;
            break;

        }


  }
  }
  }

但这对我没有任何好处,我该怎么办?柜台要么不显示任何内容,要么显示' 1'但没有别的吗?

4 个答案:

答案 0 :(得分:2)

你是否尝试过循环时在你外面声明你的计数器。与你的int i1 i2等一起输入int counter;并将其初始化为0.它应该工作。

答案 1 :(得分:2)

你在if中声明变量counter,并在其中初始化为0! 必须声明变量计数器并在while之前将其初始化为0.

while(loop=true)

你正在影响一个循环的值,而不是比较! 此外,比较true == true,而(loop)就足够了。

答案 2 :(得分:0)

对代码进行了一些更改。 在正确范围内声明变量的主要问题。

/**
* Write a description of class dummygameclass here.
* 
* @author (your name) 
 * @version (a version number or a date)
 */

import java.util.*;
public class dummygameclass {

  // instance variables - replace the example below with your own
  public static void main(String [] args) throws InterruptedException{

    Scanner reader = new Scanner(System.in);
    int i1, i2, i3, i4, i5, i6, i7, i8, i9;
    String s1, s2, s3, s4, s5, s6, s7, s8, s9;
    boolean loop = true;

    int counter = 0; // moved to outside of the loop scope        

    while(loop=true){
        System.out.println("You walk up the stairs, but you shockingly see yourself back in the same room, same position, climb again?");
        s3 = reader.next();

        if(s3.equals("y")){
            //System.out.println("You walk up the stairs, but you shockingly see yourself back in the same room, same position, climb again?");
            //s3 = reader.next();
            //int counter = 0;
            counter++;
            //System.out.println(counter);
        }
        else if(s3.equals("n")){
            System.out.println("You have chosen not to climb the staircase.");
            loop=false;
            //break; // don't need both break and setting loop to false
        }
    }

    // moved to outside of the loop scope, printed once after loop ends
    System.out.println(counter);

  }

}

答案 3 :(得分:0)

while(loop==true)

while(loop)