2014-11-26 7 views
-2

누구든지 카운터 값을 증가시킨 후 전송하는 방법을 알고 있습니까? 그래서 만약 당신이 그것을 바로 다음 방법에 하나 변경?다른 방법으로 데이터를 가져 오는 방법은 무엇입니까?

package swag; 
import java.util.Scanner; 

public class Main { 
    public static void enter(){ 
     System.out.print("welcome to the impossibe game! Press enter to start."); 

     Scanner input = new Scanner(System.in); 
     String enter = input.nextLine();  

     if (enter.equals("")){ 
      System.out.println("    Level one"); 
     }else{ 
      System.out.println("Please press enter"); 
     } 
    } 

    public static void firstlevel(){ 
     System.out.println("What is the tenth digit of PI?"); 

     Scanner input = new Scanner(System.in); 
     int awnser = input.nextInt(); 
     int awnser1 = 5; 
     int counter = 0; 

     if (awnser == awnser1){ 
      System.out.println("Correct!"); 

      counter++; 
      System.out.println("   Score: " +counter + "/1"); 

     }else{ 
      System.out.println("Wrong!"); 
      System.out.println("   Score:"+ counter+"/1"); 
     } 
    } 

    public static void secondlevel(){ 
     System.out.println("a king and queen get on a boat. then the boat sinks. how many people are alive"); 
     Scanner input = new Scanner(System.in); 
     String awnser = input.nextLine(); 

     if (awnser.equals("two ")){ 
      System.out.println("  Correct!"); 
     } 
    } 

    public static void main(String args[]){ 
     enter(); 
     firstlevel(); 
    } 
} 
+3

보고하시기 바랍니다 게시, 그것이 틀린 것처럼 보입니다. "질문하는 방법"페이지는 게시물을 게시하기 전에 귀하의 게시물을 읽도록 알려줍니다. 그것은 낄낄 거림을위한 것이 아니라 사람들이 실제로 당신의 게시물을 읽을 수 있어야합니다 =) –

답변

0

아, 카운터 정의 방법은 firstLevel에서만 볼 수 있습니다.

가장 좋은 방법은 '클래스 변수'로 만드는 것입니다. 그 방법은 다음과 같습니다.

  1. firstLevel 메서드에서 int counter = 0;을 삭제하십시오.
    public class Main { 
        static int counter = 0; 
    

    지금 카운터 모든 방법에서 볼 수 있습니다 :
  2. 는 같은 클래스의 시작이 보일 것입니다 public class Main {

후 바로 다음 줄에 static int counter = 0;을 추가합니다.

0

user384842처럼 지적하면 정적 카운터를 만들 수 있습니다. 당신이 이미 수업에 익숙하다면 나는 모른다. 그러나 이것은 실제로 당신이 수업을 사용하고자하는 좋은 예이다. 먼저 주 수업. 이 모양을 아름답게 간단한 보면 지금

public static void main(String args[]){ 
    // lets create a new Game object. it will keep track of the counter itself! 
    Game game = new Game(); 
    // then we only have to call the methods defined below.. 
    game.enter(); 
    game.firstLevel(); 
    game.secondlevel(); 
} 

모든 논리가 포함 된 클래스 게임에 대한 코드 :이 혼란보다 더 많은 도움이되었습니다 바랍니다

public class Game{ 
    // some static final members. they are not meant to change throughout the execution of the program! 
    // the advantage of defining INDENTAION just once, is that you can easily change it in one place and it will always be consistent! 
    private static final String INDENTAION = "   "; 
    private static final int TOTAL_POINTS = 2; 
    // we can use the same scanner object in each method! 
    private Scanner input = new Scanner(System.in); 
    // just like the counter. it will be accessible in each method and refer to the same integer! 
    private int counter = 0; 

    public void enter(){ 
     System.out.print("welcome to the impossibe game! Press enter to start."); 

     Scanner input = new Scanner(System.in); 
     String enter = input.nextLine(); 

     if (enter.equals("")){ 
      System.out.println(INDENTAION+"Level one"); 
     }else{ 
      // i am not sure why you put this here, but i'll leave it in for now 
      System.out.println("Please press enter"); 
     } 
    } 

    // we put that code into seperate methods, since it will get called multiple times! 
    private void answerCorrect(){ 
     System.out.println("Correct!"); 
     counter++; 
     printScore(); 
    } 

    private void answerWrong(){ 
     System.out.println("Wrong!"); 
     printScore(); 
    } 

    private void printScore(){ 
     System.out.println(INDENTAION + "Score: " + counter +"/"+ TOTAL_POINTS); 
    } 

    public void firstLevel(){ 
     System.out.println("What is the tenth digit of PI?"); 
     int awnser = input.nextInt(); 

     if (awnser == 5){ 
      answerCorrect(); 
     }else{ 
      answerWrong(); 
     } 
    } 

    public void secondlevel(){ 
     System.out.println("a king and queen get on a boat. then the boat sinks. how many people are alive"); 
     String awnser = input.nextLine(); 

     if (awnser.equals("two") || awnser.equals("2")){ 
      answerCorrect(); 
     }else{ 
      answerWrong(); 
     } 
    } 
} 

을 :)

관련 문제