java - 当我请求整数时,如何检查用户输入是否包含字符串?

标签 java

在我的代码中,我要求用户输入整数。我试图弄清楚如何检查用户输入的输入是否为非整数。它适用于我不想要的任何数字,但我希望在用户尝试输入字母或单词后编写一份声明。有没有办法使用 if((coin1.getValue()!=int)) 这样的 if 语句来做到这一点?我试图解决这个问题的代码位于我的 CoidDriver 类中。

这是我的父类(super class):

public class Coin {
private final int HEADS = 0;
private final int TAILS = 1;

private int face;

//-----------------------------------------------------------------
//  Sets up the coin by flipping it initially.
//-----------------------------------------------------------------
public Coin ()
{
   flip();
}

//-----------------------------------------------------------------
//  Flips the coin by randomly choosing a face value.
//-----------------------------------------------------------------
public void flip ()
{
   face = (int) (Math.random() * 2);
}

//-----------------------------------------------------------------
//  Returns true if the current face of the coin is heads.
//-----------------------------------------------------------------
public boolean isHeads ()
{
   return (face == HEADS);
}

//-----------------------------------------------------------------
//  Returns the current face of the coin as a string.
//-----------------------------------------------------------------
public String toString()
{
   String faceName;

   if (face == HEADS)
      faceName = "Heads";
   else
      faceName = "Tails";

   return faceName;
}
}

这是我 child 的类(class)

public class MonetaryCoin extends Coin
{ 
public int coinValue;

public void setValue(int coinValue){

    this.coinValue= coinValue;
}
public int getValue(){

    return coinValue;
}

public void flip(){

    super.toString();
    super.flip();
}

}

这是我的司机

import java.util.Scanner;

public class CoinDriver{


public static void main(String[] args) {


int heads = 0, tails = 0, numFlips=0;

String tryFlip="yes";
Scanner scan = new Scanner(System.in);

MonetaryCoin coin1 = new MonetaryCoin();
MonetaryCoin coin2 = new MonetaryCoin();
MonetaryCoin coin3 = new MonetaryCoin();

System.out.println("How much are your coins worth?");

System.out.println("Please enter the value of your first coin");

coin1.setValue(scan.nextInt());
if(coin1.getValue()==1||coin1.getValue()==5||coin1.getValue()==10||coin1.getValue()==25||coin1.getValue()==50||coin1.getValue()==100){

System.out.println("Please enter the value of your second coin");}

else if((coin1.getValue()!=1)||(coin1.getValue()!=5)||(coin1.getValue()!=10)||(coin1.getValue()!=25)||(coin1.getValue()!=50)||(coin1.getValue()!=100)){

while((coin1.getValue()!=1)||(coin1.getValue()!=5)||(coin1.getValue()!=10)||(coin1.getValue()!=25)||(coin1.getValue()!=50)||(coin1.getValue()!=100)){
            System.out.println("Invalid must enter real coins");
            System.out.println("Please re-enter the value of your first coin");

            coin1.setValue(scan.nextInt());
            if(coin1.getValue()==1||coin1.getValue()==5||coin1.getValue()==10||coin1.getValue()==25||coin1.getValue()==50||coin1.getValue()==100)
                break;
    }
    System.out.println("Please enter the value of your second coin");

}
else{}

    coin2.setValue(scan.nextInt());

if(coin2.getValue()==1||coin2.getValue()==5||coin2.getValue()==10||coin2.getValue()==25||coin2.getValue()==50||coin2.getValue()==100){

System.out.println("Please enter the value of your third coin");}

else if((coin2.getValue()!=1||coin2.getValue()!=5||coin2.getValue()!=10||coin2.getValue()!=25||coin2.getValue()!=50||coin2.getValue()!=100)){

    while(coin2.getValue()!=1||coin2.getValue()!=5||coin2.getValue()!=10||coin2.getValue()!=25||coin2.getValue()!=50||coin2.getValue()!=100){
        System.out.println("Invalid must enter real coins");
        System.out.println("Please re-enter the value of your second coin");

        coin2.setValue(scan.nextInt());
        if(coin2.getValue()==1||coin2.getValue()==5||coin2.getValue()==10||coin2.getValue()==25||coin2.getValue()==50||coin2.getValue()==100)
            break;
    }
    System.out.println("Please enter the value of your third coin");

}
else{}
coin3.setValue(scan.nextInt());


if(coin3.getValue()==1||coin3.getValue()==5||coin3.getValue()==10||coin3.getValue()==25||coin3.getValue()==50||coin3.getValue()==100){
    System.out.println("Value of all three of your coins are worth: " + (coin1.getValue()+coin2.getValue()+coin3.getValue())+" cents.");

System.out.println("Well, aren't you rich?");

}
else if(coin3.getValue()!=1||coin3.getValue()!=5||coin3.getValue()!=10||coin3.getValue()!=25||coin3.getValue()!=50||coin3.getValue()!=100){
while(coin3.getValue()!=1||coin3.getValue()!=5||coin3.getValue()!=10||coin3.getValue()!=25||coin3.getValue()!=50||coin3.getValue()!=100){
    System.out.println("Invalid must enter real coins");
    System.out.println("Please re-enter the value of your third coin");
    coin3.setValue(scan.nextInt());

    if(coin3.getValue()==1||coin3.getValue()==5||coin3.getValue()==10||coin3.getValue()==25||coin3.getValue()==50||coin3.getValue()==100)
        break;
}
System.out.println("Value of all three of your coins are worth: " + (coin1.getValue()+coin2.getValue()+coin3.getValue())+" cents.");

System.out.println("Well, aren't you rich?");
}
else{}


System.out.println("");


System.out.println("Would you like to flip your coin? yes/no");

tryFlip=scan.next();
while(tryFlip.equalsIgnoreCase("yes")){


    coin1.flip();

    numFlips++;
    if (coin1.isHeads())
     heads++;

     else
     tails++;

     System.out.println("");
     System.out.println ("You Got:     "+ coin1.toString()+"!!!!");
     System.out.println("");
     System.out.println ("The number flips: " + numFlips);
     System.out.println ("The number of heads: " + heads);
     System.out.println ("The number of tails: " + tails);

     System.out.println("Would you like to flip again? yes/no");
     tryFlip=scan.next();
}
}



}

这是新的驱动程序

import java.util.Scanner;

public class CoinDriver{


public static void main(String[] args) {


int heads = 0, tails = 0, numFlips=0;

String tryFlip="yes";
Scanner scan = new Scanner(System.in);

MonetaryCoin coin1 = new MonetaryCoin();
MonetaryCoin coin2 = new MonetaryCoin();
MonetaryCoin coin3 = new MonetaryCoin();

System.out.println("How much are your coins worth?");

System.out.println("Please enter the value of your first coin");


if(scan.hasNextInt())
    coin1.setValue(scan.nextInt());
 else {
    System.out.println("C'mon, you need to enter an int!");
    scan.next(); // clear their garbage response
}
if(coin1.getValue()==1||coin1.getValue()==5||coin1.getValue()==10||coin1.getValue()==25||coin1.getValue()==50||coin1.getValue()==100){

System.out.println("Please enter the value of your second coin");}

else if((coin1.getValue()!=1)||(coin1.getValue()!=5)||(coin1.getValue()!=10)||(coin1.getValue()!=25)||(coin1.getValue()!=50)||(coin1.getValue()!=100)){

while((coin1.getValue()!=1)||(coin1.getValue()!=5)||(coin1.getValue()!=10)||(coin1.getValue()!=25)||(coin1.getValue()!=50)||(coin1.getValue()!=100)){
            System.out.println("Invalid must enter real coins");
            System.out.println("Please re-enter the value of your first coin");

            if(scan.hasNextInt())
                coin1.setValue(scan.nextInt());
             else {
                System.out.println("I remember my first coin");
                scan.next(); // clear their garbage response
            }
            if(coin1.getValue()==1||coin1.getValue()==5||coin1.getValue()==10||coin1.getValue()==25||coin1.getValue()==50||coin1.getValue()==100)
                break;
    }
    System.out.println("Please enter the value of your second coin");

}
else{}

if(scan.hasNextInt())
    coin2.setValue(scan.nextInt());
 else {
    System.out.println("Are you serious right now?");
    scan.next(); // clear their garbage response
}

if(coin2.getValue()==1||coin2.getValue()==5||coin2.getValue()==10||coin2.getValue()==25||coin2.getValue()==50||coin2.getValue()==100){

System.out.println("Please enter the value of your third coin");}

else if((coin2.getValue()!=1||coin2.getValue()!=5||coin2.getValue()!=10||coin2.getValue()!=25||coin2.getValue()!=50||coin2.getValue()!=100)){

    while(coin2.getValue()!=1||coin2.getValue()!=5||coin2.getValue()!=10||coin2.getValue()!=25||coin2.getValue()!=50||coin2.getValue()!=100){
        System.out.println("Invalid must enter real coins");
        System.out.println("Please re-enter the value of your second coin");

        if(scan.hasNextInt())
            coin2.setValue(scan.nextInt());
         else {
            System.out.println("Alien coins are the best");
            scan.next(); // clear their garbage response
        }
        if(coin2.getValue()==1||coin2.getValue()==5||coin2.getValue()==10||coin2.getValue()==25||coin2.getValue()==50||coin2.getValue()==100)
            break;
    }
    System.out.println("Please enter the value of your third coin");

}
else{}
if(scan.hasNextInt())
    coin3.setValue(scan.nextInt());
 else {
    System.out.println("Seems Legit");
    scan.next(); // clear their garbage response
}


if(coin3.getValue()==1||coin3.getValue()==5||coin3.getValue()==10||coin3.getValue()==25||coin3.getValue()==50||coin3.getValue()==100){
    System.out.println("Value of all three of your coins are worth: " + (coin1.getValue()+coin2.getValue()+coin3.getValue())+" cents.");

System.out.println("Well, aren't you rich?");

}
else if(coin3.getValue()!=1||coin3.getValue()!=5||coin3.getValue()!=10||coin3.getValue()!=25||coin3.getValue()!=50||coin3.getValue()!=100){
while(coin3.getValue()!=1||coin3.getValue()!=5||coin3.getValue()!=10||coin3.getValue()!=25||coin3.getValue()!=50||coin3.getValue()!=100){
    System.out.println("Invalid must enter real coins");
    System.out.println("Please re-enter the value of your third coin");
    if(scan.hasNextInt())
        coin3.setValue(scan.nextInt());
     else {
        System.out.println("Sick coins");
        scan.next(); // clear their garbage response
    }

    if(coin3.getValue()==1||coin3.getValue()==5||coin3.getValue()==10||coin3.getValue()==25||coin3.getValue()==50||coin3.getValue()==100)
        break;
}
System.out.println("Value of all three of your coins are worth: " + (coin1.getValue()+coin2.getValue()+coin3.getValue())+" cents.");

System.out.println("Well, aren't you rich?");
}
else{}


System.out.println("");


System.out.println("Would you like to flip your coin? ");
System.out.println("Type yes to continue or press any key to end");

if(tryFlip.equalsIgnoreCase("yes"))


while(tryFlip.equalsIgnoreCase("yes")){


    coin1.flip();

    numFlips++;
    if (coin1.isHeads())
     heads++;

     else
     tails++;

     System.out.println("");
     System.out.println ("You Got:     "+ coin1.toString()+"!!!!");
     System.out.println("");
     System.out.println ("The number flips: " + numFlips);
     System.out.println ("The number of heads: " + heads);
     System.out.println ("The number of tails: " + tails);

     System.out.println("Would you like to flip again?");
     System.out.println("Type yes to continue or press any key to end");

     tryFlip=scan.next();
}
System.out.println("See you next time!");
}

再次感谢您的好意!

最佳答案

由于您使用的是扫描仪,因此您可以执行以下操作:

if(scan.hasNextInt()) {
    coin1.setValue(scan.nextInt());
} else {
    System.out.println("C'mon, you need to enter an int!");
    sc.next(); // clear their garbage response
}

关于java - 当我请求整数时,如何检查用户输入是否包含字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20108878/

相关文章:

java - 如何在java中打开另一个目录中的文件?

java - 如何获取 Activity 的 View ?

java - 如何创建一个使用 Java 中的二叉搜索树获取前一个节点的方法?

Java调整嵌套Hashmap值

java - 如何在 jFreeChart TimeSeries 中开始和结束值 "0"的每个系列?

javaFX:使用translatetransition移动具有绝对坐标的形状

java - 不兼容类型错误 String[]

java - 这可以只用递归来完成吗?

java - 我想在启动后从另一个服务调用服务方法,但我不知道如何创建该服务的对象

java - 处理父类(super class)型的所有子类型