2015-10-17 41 views
0

我正在嘗試創建一個計數器,以便只要該人在循環過程中一直保持響應,就會繼續計數,然後在每個響應之後說明該人已經說了多少次了。 (y)在這種情況下。Java中的計數器(非GUI)

這是迄今爲止代碼:

/** 
* 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',但沒有別的?

+2

你設置計數器= 0每次循環運行。 –

+1

這顯然是功課。所以我會提供一些一般建議,而不是爲你做。不要在每個方法的開始創建9個相同名稱的變量。給你的變量有意義的名字。此外,嘗試對齊代碼的行,以便大括號確實匹配。這將使您的代碼塊更易於調試。 – zerobandwidth

+1

@zerobandwidth,實際上我還在上高中。這是有趣的課外活動! – Amad27

回答

2

您是否嘗試過在循環中聲明您的計數器?伴隨着你的int i1 i2等類型int計數器;並將其初始化爲0.它應該可以工作。

2

你在if中聲明變量計數器並且初始化爲0! 變量計數器必須在時間之前聲明並初始化爲0。

while(loop=true) 

您正在影響循環的值,而不是比較! 此外,比較true == true是沒有用的,而(loop)就足夠了。

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); 

    } 

}