Java 如何更改文本颜色和最终随机变量?

Java 如何更改文本颜色和最终随机变量?,java,random,Java,Random,对于Java类,我必须做一个简单的猜字母游戏,我想使用字符串将字母设置为随机的,但是对于类,这需要是一个最终变量。我该怎么做? 这个词也需要是橙色文本。有什么办法可以做到这一点吗 import java.util.Scanner; import java.awt.Color; import java.util.Random; public class PracTestRand{ public static void main(String[] args){ Random r

对于Java类,我必须做一个简单的猜字母游戏,我想使用字符串将字母设置为随机的,但是对于类,这需要是一个最终变量。我该怎么做? 这个词也需要是橙色文本。有什么办法可以做到这一点吗

 import java.util.Scanner;
 import java.awt.Color;
 import java.util.Random;


public class PracTestRand{
public static void main(String[] args){

    Random r = new Random();
    String secret = "";
    secret = (secret + (char) (r.nextInt(26) + 'a'));
    String stop = "go";
    Scanner sc = new Scanner(System.in);
    System.out.print("Guess the secret letter!\n");



    while (stop.equalsIgnoreCase("go")){

        System.out.print("Enter a word ('stop' to end): ");

        String word = sc.nextLine();


        if (word.contains(secret)){
           System.out.println("That contains the secret letter!");

        }   

        else { 
        System.out.println("Your word does not contain the secret letter!");

        } 

        if (word.equals(secret)){
            System.out.println("You guessed the secret letter!\nThanks!");
            stop = "stop";
        }

        if (word.equals("stop")){
            stop = "stop";
            System.out.println("Thanks!");
        }

    } 
}

我假设您希望
secret
成为
final
。请注意,
final
允许初始化变量,因此只需确保使用所需的值将其初始化为
final

final String value;
value = "Your final string";
就你而言:

final String secret;
secret = String.valueOf((char) (r.nextInt(26) + 'a'));
或者在一行中:

final String secret = String.valueOf((char) (r.nextInt(26) + 'a'));
使用
System.out.print()或
System.out.println()时,无法使用颜色。如果需要着色,您可能需要考虑使用.</P>开发GUI。

还有,您所提供的代码有几个问题,请考虑下面的注释:

import java.util.Scanner;
import java.util.Random;

public class PracTestRand{

  public static void main(String[] args){
    Random r = new Random();
    //Just make it a one-line statement and final will give you no problem.
    final String secret = String.valueOf((char) (r.nextInt(26) + 'a'));
    //You are using a String as a boolean ("stop" -> false & "go" -> true).
    boolean keepPlaying = true;
    Scanner sc = new Scanner(System.in);
    //Use println() here instead of print() + "\n"
    System.out.println("Guess the secret letter!");
    while (keepPlaying) {
        System.out.print("Enter a word ('stop' to end): ");
        String word = sc.nextLine();
        /*
         * Control flow - It seems you only want one of the statements to
         * be executed per iteration, use 'else if' to make sure 
         * only one is executed.
         * I've also replaced equals() with equalsIgnoreCase()
         * and added toLowerCase()
         * This allows you to play the game with upper case letters.
         */
        if (word.equalsIgnoreCase("stop")){
            System.out.println("Thanks!");
            keepPlaying = false;
        }
        else if (word.equalsIgnoreCase(secret)){
            System.out.println("You guessed the secret letter!");
            System.out.println("Thanks!");
            keepPlaying = false;
        }
        else if (word.toLowerCase().contains(secret)){
           System.out.println("That contains the secret letter!");
        }   
        else { 
            System.out.println("Your word does not contain the secret letter!");
        } 
    } 
    //It's recommended practice to close the Scanner after you are done
    sc.close();
  }
}

无法(可移植地)为使用
System.out.println
打印的文本指定颜色。此外,我没有看到任何
final
变量,也没有看到问题的实质。也许你可以再详细解释一下。谢谢你,伙计!这对初学者来说是个很大的帮助!:)