我怎样才能改变int值? [英] How would i be able to change an int value?

查看:125
本文介绍了我怎样才能改变int值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个小程序,用户必须猜出一个数字。我希望他们输入他们猜测的数字,然后将玩家插入的值分配给变量x以检查这是否是正确的值。如何获取插入的值并将其分配给x变量?

I am writing a small program where the user has to guess a number. I want them to type in the number that they are guessing and then assign the value inserted by the player to the variable x to check if this is the right value. How could I get the inserted value and assign it to the x variable?

这是我到目前为止所做的:

This is what I have so far:

public static void main(String[] args) {

    Scanner textIn = new Scanner(System.in);
    System.out.println("Try to guess what number I am thinking of.");
    //X is the int I want to change
    int x = 100;

    //Z is the one I am comparing x to
    int z = 10;


    String zGuess = textIn.nextLine();
    boolean xTest = true;
    {
        if (x == z);
        System.out.println("You guessed right!");
    }
    //XTEST PART ONE   
    while (x < z) {
        System.out.println("X < Z");
        break;
    }
    //XTEST PART TWO   
    while (x > z) {
        System.out.println("X > Z");
        break;
    }
}


推荐答案

这里是一个简单的解决方案在while循环中环绕它并始终设置 x = textIn.nextInt()

Here is a simple solution. Surround it in a while loop and always set x = textIn.nextInt()

    public static void main(String[] args) {
    Scanner textIn = new Scanner(System.in);
    System.out.println("Try to guess what number I am thinking of.");
    //X is the int I want to change
    int x = -1;

    //Z is the one I am comparing x to
    int z = 10;

    while(x != z)
    {
        x = textIn.nextInt();
        if(x == z)
        {
            System.out.println("You got it right!");
        }

        else if(x < z)
        {
            System.out.println("Try a higher number.");
        }
        else
        {
            System.out.println("Try a lower number.");
        }

    }
    System.out.println("Great job!")
}

这篇关于我怎样才能改变int值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆