-1
package com.company;

import javax.xml.bind.SchemaOutputResolver;

class Main {

public static void main(String[] args)
        throws java.io.IOException {
    char x, y;
    x = 'T';
    System.out.println("*GAME START");
    for(int i=5;i>=0;--i) {
        System.out.println("You Have " + i + " Life");
            y = (char) System.in.read();
            if (y == x)
            {
                System.out.println("**WIN**");
                break;
            }
            else
                {
            if(y>x)
                System.out.println("X<Z");
            else
                System.out.println("X>Z");
            }
        }
    }
}

Can you tell me what's wrong in this code? What compilation problem.

*GAME START
You Have 5 Life
A
X>Z
You Have 4 Life
X>Z
You Have 3 Life

Why does the code not ask me a new font when print "You Have 4 Life"? It prints "You Have 3 Life" then I can read a new font.

Andrew Thompson
  • 163,965
  • 36
  • 203
  • 405

2 Answers2

0

It is good practice to use debugger in such cases.

System.in.read() read only one symbol at a time. First is 'A' and second is '\n'. On next loop application will ask you for the next input.

You could look for other implementations Take a char input from the Scanner .

uli
  • 641
  • 5
  • 15
0

System.in.read reads the next byte of data from the input stream. If you enter any character and press return, the char and the linefeed/carraige return are in the input stream. So skip the Return key.

do 
{
    y = (char) System.in.read();
}
while (!Character.isLetterOrDigit(y)); 
milbrandt
  • 1,273
  • 2
  • 12
  • 19