JAVA神手幫我解一下~~急

Design and implement an application that reads a string from the user and prints it one character per line.

讓使用者輸入一個字串,將字串的每一個字元分行列印出來

EX: 使用者輸入: abcde,

Print:

a

b

c

d

e

,

Design and implement an application that plays the Hi-Lo guessing game with numbers. The program should pick a random number between 1 and 100(inclusive), then repeatedly prompt the user to guess the number. On each guess, report to the user that he or she is correct or that the guess is high or low. Continue accepting guesses until the user guesses correctly or choose to quit. Use a sentinel value to determine whether the user wants to quit. Count the number of guesses and report that value when the user guesses correctly. At the end of each game(by quitting or a correct guess), prompt to determine whether the user wants to play again. Continue playing games until the user chooses to stop.

製作一猜數字的遊戲(範圍1~100)

使用者每猜一次,猜錯的話要告訴使用者他猜的數字是大於或是小於謎底

讓使用者可重複做猜數字的動作直到猜對為止或是使用者選擇結束(設定一個結束的指令 EX:使用者輸入Q即為結束)

每當使用者猜對數字或是選擇結束後 詢問使用者是否要繼續玩

是的話 重新讓使用者玩遊戲直到使用者選擇不玩為止

1 Answer

Rating
  • 1 decade ago
    Favorite Answer

    請參考我的做法

    import java.util.*;public class Y9611 { public static void main(String[] args) { Q1(); Q2(); } public static void Q1() { Scanner scan = new Scanner(System.in); System.out.print("Please input a string: "); String s = scan.next(); for (int i = 0; i < s.length(); i++) { System.out.println(s.charAt(i)); } } public static void Q2() { Scanner scan = new Scanner(System.in); while (true) { final int x = (int)(Math.random() * 100 + 1); System.out.println("A random number (1~100) is generated."); while (true) { System.out.print("Please take a guess ('Q' to stop): "); String s = scan.next(); if (s.equalsIgnoreCase("Q")) break; int a = Integer.parseInt(s); if (a == x) { System.out.println(a + " is the correct answer!"); break; } else if (a > x) { System.out.println(a + " is too high."); } else { System.out.println(a + " is too low."); } } System.out.print("Continue (y/n)? "); String s = scan.next(); if (s.equalsIgnoreCase("n")) break; } }}

Still have questions? Get your answers by asking now.