Java 如何将字符串解析为整数

Java 如何将字符串解析为整数,java,Java,好的,我有我的石头,布,剪刀游戏工作。除了Q退出外,其他都有效。由于我的扫描器只接受整数,我如何才能将“Q”字符串传递给它。我假设我只需在while循环中添加一个简单的if(string.equals(“Q”){break;},我就可以开始了。让我知道你的想法 import java.util.Scanner; import java.util.Random; public class RockPaperScissors { /** * (Insert a brief de

好的,我有我的石头,布,剪刀游戏工作。除了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();


        }
    }
}
import java.util.Scanner;
导入java.util.Random;
公营剪纸机
{
/**
*(插入描述该方法用途的简要说明)
* 
*@param args
*/
公共静态void main(字符串[]args)
{
整数compint;
字符串usermove=“”;
字符串compmove=“”;
字符串赢家=”;
整数计数=0;
int输入=0;
扫描仪输入=新扫描仪(系统输入);
Random gen=新的Random();
系统输出打印(“输入石头(1)、纸(2)、剪刀(3){Q退出]:”;
input=in.nextInt();
而(计数<3)
{
compint=gen.nextInt(3)+1;
如果(输入=1)
{
usermove=“Rock”;
}
else if(输入=2)
{
usermove=“纸张”;
}
else if(输入=3)
{
usermove=“剪刀”;
}
如果(compint==1)
{
compmove=“岩石”;
}
else if(compint==2)
{
compmove=“纸张”;
}
else if(compint==3)
{
compmove=“剪刀”;
}
如果(compint==输入)
{
获胜者=“平局”;
}
else if(compint==1&&input==3)
{
winner=“计算机”;
}
else if(compint==2&&input==1)
{
winner=“计算机”;
}
else if(compint==3&&input==2)
{
winner=“计算机”;
}
其他的
{
winner=“用户”;
}
系统输出打印(“计算机:+compmove+“|”);
System.out.print(“您:“+usermove+”)”;
System.out.println(“获胜者:+Winner”);
System.out.println();
计数++;
系统输出打印(“输入石头(1)、纸(2)、剪刀(3){Q退出]:”;
input=in.nextInt();
}
}
}
Integer.parseInt(myString)

只需插入myString

class JTest
{
    public static void main(String[] args)
    {
        String a = "5";
        int b = Integer.parseInt(a);
        System.out.println(b);
    }
}

这个代码对我有用:

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


        }
    }
}
package.com.sandbox;
导入java.util.Random;
导入java.util.Scanner;
公共类沙箱{
/**
*(插入描述该方法用途的简要说明)
*
*@param args
*/
公共静态void main(字符串[]args){
整数compint;
字符串usermove=“”;
字符串compmove=“”;
字符串赢家=”;
整数计数=0;
字符串输入=null;
int输入=0;
扫描仪输入=新扫描仪(系统输入);
Random gen=新的Random();
系统输出打印(“输入石头(1)、纸(2)、剪刀(3){Q退出]:”;
rawInput=in.next();
如果(“Q”。等于(输入)){
return;//退出main
}
输入=整数.parseInt(rawInput);
而(计数<3){
compint=gen.nextInt(3)+1;
如果(输入=1){
usermove=“Rock”;
}else if(输入=2){
usermove=“纸张”;
}else if(输入=3){
usermove=“剪刀”;
}
如果(compint==1){
compmove=“岩石”;
}else if(compint==2){
compmove=“纸张”;
}else if(compint==3){
compmove=“剪刀”;
}
如果(compint==输入){
获胜者=“平局”;
}else if(compint==1&&input==3){
winner=“计算机”;
}else if(compint==2&&input==1){
winner=“计算机”;
}else if(compint==3&&input==2){
winner=“计算机”;
}否则{
winner=“用户”;
}
系统输出打印(“计算机:+compmove+“|”);
System.out.print(“您:“+usermove+”)”;
System.out.println(“获胜者:+Winner”);
System.out.println();
计数++;
系统输出打印(“输入石头(1)、纸(2)、剪刀(3){Q退出]:”;
input=in.nextInt();
}
}
}
我所做的是将输入作为字符串。我将其保存到名为
rawInput
的变量中。然后我检查它是否等于“Q”。如果等于,我退出。如果不是,我将其转换为整数并使用剩下的逻辑

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

您有两个选择:

  • 使用数值退出(0或-1),或
  • 将程序转换为同时接受字符串和数字。方法是通过

    //这假定输入是字符串类型
    int选项=0;
    系统输出打印(“输入石头(1)、纸(2)、剪刀(3){Q退出]:”;
    input=in.nextLine();
    试一试{
    option=Integer.parseInt(输入);
    }捕获(NumberFormatException nfe){
    //不是一个数字,所以它要么是虚假输入,要么是退出选项。
    if(“Q”。相等信号情况(输入)){
    System.out.println(“退出”);
    系统出口(0);
    }否则{
    System.out.println(“伪输入”);
    }
    }
    而(计数<3&&选项!=0){
    //逻辑
    }
    

如果用户输入的不是0-9和“q”或“q”,那么它应该可以工作:

Scanner sc = new Scanner(System.in);
   String a = sc.next();
   if(a.equalsIgnoreCase("q")){
       System.out.println("quit game");
   }    
   else{
       int input = Integer.parseInt(a);
   }

按如下方式更改您的程序:

public static void main(String... args) {

        Scanner in = new Scanner(System.in);
        int compint;
        String usermove = "";
        String compmove = "";
        String winner = "";
        int count = 0;
        int input = 0;
        Random gen = new Random();
        Set<Boolean> set = new HashSet<Boolean>();
        boolean contains = false;
        while (count < 3) {
            System.out.print("Enter Rock(1), Paper(2), Scissors(3) {Q to quit]: ");
            String nextLine=in.next().trim();
            do {
                for (int index = 0; index < nextLine.length(); index++) {
                    set.add(Character.isLetter(nextLine.charAt(index)));
                }
                contains = set.contains(true);
                if(contains) {
                if (nextLine.equals("Q")) {
                    System.out.println("program exited");
                    return;
                } else {
                    System.out .print("Re-enter Rock(1), Paper(2), Scissors(3) {Q to quit]: ");
                    nextLine=in.next();
                }
                } else {
                    input=Integer.parseInt(nextLine);
                }

                set.remove(true);
            } while (contains);

            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.println("program's end");
    }
公共统计局
public static void main(String... args) {

        Scanner in = new Scanner(System.in);
        int compint;
        String usermove = "";
        String compmove = "";
        String winner = "";
        int count = 0;
        int input = 0;
        Random gen = new Random();
        Set<Boolean> set = new HashSet<Boolean>();
        boolean contains = false;
        while (count < 3) {
            System.out.print("Enter Rock(1), Paper(2), Scissors(3) {Q to quit]: ");
            String nextLine=in.next().trim();
            do {
                for (int index = 0; index < nextLine.length(); index++) {
                    set.add(Character.isLetter(nextLine.charAt(index)));
                }
                contains = set.contains(true);
                if(contains) {
                if (nextLine.equals("Q")) {
                    System.out.println("program exited");
                    return;
                } else {
                    System.out .print("Re-enter Rock(1), Paper(2), Scissors(3) {Q to quit]: ");
                    nextLine=in.next();
                }
                } else {
                    input=Integer.parseInt(nextLine);
                }

                set.remove(true);
            } while (contains);

            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.println("program's end");
    }