C
C
Chubaka2022-04-02 09:18:58
Java
Chubaka, 2022-04-02 09:18:58

Why can't a variable be subtracted inside a while?

import java.util.*;
class Practice2{
  static Scanner eva = new Scanner(System.in);
  public static void main(String[] args) {
  int x, y,over,account;
  over = eva.nextInt();//овер(максимально допустимый "минус")
  account = eva.nextInt();// общий счёт потребителя	
  
  while(account >= over) {
  x = eva.nextInt();// если положителььное число, то +, иначе -
  if(x<0)
    account-=x;
  else
    account+=x;	
    
  System.out.println("Ваш текущий счёт : "+account);
  }
  
  
  }
}

In order not to try to enter, in any case, ONLY addition occurs, there is no subtraction at all :(

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Michael, 2022-04-02
@BladehelpRunner

Because the computer does what you told it to do, not what you want it to do :)
Let account = 100
then if x=10
account = 100 + 10 = 110
then if x = -5
account = 110 - (- 5) = ?

D
Denis, 2022-04-03
@DDDenis

account = account - x;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question