gpt4 book ai didi

java - 如何将字符串解析为整数

转载 作者:行者123 更新时间:2023-12-02 07:17:14 24 4
gpt4 key购买 nike

好的,我的石头剪刀布游戏可以运行了。除了 Q 退出之外,其他都有效。由于我的扫描仪只接受整数,我如何将“Q”字符串传递给它。我假设我只需在 while 循环中添加一个简单的 if(string.equals("Q") {break;} 就可以了。让我知道您的想法。

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

public class RockPaperScissors
{

/**
* (Insert a brief description that describes the purpose of this method)
*
* @param args
*/
public static void main(String[] args)
{
int compint;
String usermove = "";
String compmove = "";
String winner = "";
int count = 0;
int input=0;


Scanner in = new Scanner(System.in);
Random gen = new Random();

System.out.print("Enter Rock(1), Paper(2), Scissors(3) {Q to quit]: ");
input=in.nextInt();
while (count < 3)
{
compint = gen.nextInt(3) + 1;

if (input == 1)
{
usermove = "Rock";
}
else if (input == 2)
{
usermove = "Paper";
}
else if (input == 3)
{
usermove = "Scissors";
}

if (compint == 1)
{
compmove = "Rock";
}
else if (compint == 2)
{
compmove = "Paper";
}
else if (compint == 3)
{
compmove = "Scissors";
}

if (compint == input)
{
winner = "TIE";
}
else if (compint == 1 && input == 3)
{
winner = "COMPUTER";
}
else if (compint == 2 && input == 1)
{
winner = "COMPUTER";
}
else if (compint == 3 && input == 2)
{
winner = "COMPUTER";
}
else
{
winner = "USER";
}

System.out.print("Computer: " + compmove + " | ");
System.out.print("You: " + usermove + " | ");
System.out.println("Winner: " + winner);
System.out.println();
count++;
System.out.print("Enter Rock(1), Paper(2), Scissors(3) {Q to quit]: ");
input = in.nextInt();


}
}
}

最佳答案

这段代码对我有用:

package com.sandbox;

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

public class Sandbox {

/**
* (Insert a brief description that describes the purpose of this method)
*
* @param args
*/
public static void main(String[] args) {
int compint;
String usermove = "";
String compmove = "";
String winner = "";
int count = 0;
String rawInput = null;
int input = 0;


Scanner in = new Scanner(System.in);
Random gen = new Random();

System.out.print("Enter Rock(1), Paper(2), Scissors(3) {Q to quit]: ");
rawInput = in.next();
if ("Q".equals(rawInput)) {
return; //exit main
}
input = Integer.parseInt(rawInput);

while (count < 3) {
compint = gen.nextInt(3) + 1;

if (input == 1) {
usermove = "Rock";
} else if (input == 2) {
usermove = "Paper";
} else if (input == 3) {
usermove = "Scissors";
}

if (compint == 1) {
compmove = "Rock";
} else if (compint == 2) {
compmove = "Paper";
} else if (compint == 3) {
compmove = "Scissors";
}

if (compint == input) {
winner = "TIE";
} else if (compint == 1 && input == 3) {
winner = "COMPUTER";
} else if (compint == 2 && input == 1) {
winner = "COMPUTER";
} else if (compint == 3 && input == 2) {
winner = "COMPUTER";
} else {
winner = "USER";
}

System.out.print("Computer: " + compmove + " | ");
System.out.print("You: " + usermove + " | ");
System.out.println("Winner: " + winner);
System.out.println();
count++;
System.out.print("Enter Rock(1), Paper(2), Scissors(3) {Q to quit]: ");
input = in.nextInt();


}
}
}

我正在做的是将输入作为字符串。我将其保存到名为 rawInput 的变量中。然后我检查它是否等于“Q”。如果是的话我就放弃了如果不是,我将其转换为整数并使用其余的逻辑。

@MadProgrammer 对于如何使此代码更具容错性提出了一些很好的建议,我会遵循这些建议,但我发布了此代码,因为它直接回答了您的问题。

关于java - 如何将字符串解析为整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14795013/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com