在我的代码中,我要求用户输入为整数。我试图找出如何检查用户输入的输入是否为非整数。它适用于我不想要的任何数字,但我希望在用户尝试输入字母或单词后写一个声明。有没有办法使用像if((coin1.getValue()!= int))这样的if语句?试图解决这个问题的代码是在我的CoidDriver类中。
这是我的超类:
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;
}
}
这是我的孩子班
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!");
}
再次谢谢你,先生!
答案 0 :(得分:3)
由于您使用的是扫描仪,因此可以执行此操作:
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
}