2015-10-03 9 views
0

Моя программа кассовой обработки почти завершена, она может обрабатывать продажи и возвращает и добавляет или вычитает это на деньги в реестре.Я почти закончен, но как петлю всю программу?

Моя единственная проблема заключается в том, что как только я закончил добавлять значения, например, программа закрывается, и я не могу понять, как ее вернуть в меню выбора. Я пробовал использовать цикл do и цикл while, но он кричал на меня, говоря, что у него был неверный ввод (вероятно, потому, что вы должны нажать F, чтобы остановить, когда вы проверяете).

Как я могу зациклить все это?

import java.util.Scanner; 
import java.util.ArrayList; 


public class Assignment3_000848913 
{ 
    public static void main(String[] args) 
    { 
     Scanner in = new Scanner(System.in); 
     ArrayList<Integer> Prices = new ArrayList<Integer>(); 
     ArrayList<Integer> ReturnPrices = new ArrayList<Integer>(); 
     int totalRegisterMoney = 0; 
     int Choice = 0; 

     System.out.print("What would you like to do?"); 
     System.out.println(); 
     System.out.print("Press 1. Process a sale"); 
     System.out.println(); 
     System.out.print("Press 2. Process a return"); 
     System.out.println(); 
     System.out.print("Press 3. Display Money in register"); 
     System.out.println(); 
     System.out.print("Press 4. Exit"); 
     System.out.println(); 
     Choice = in.nextInt(); 


     if(Choice == 1) 
     { 
      //THIS LOOP READS IN ALL THE PRICES// 
      System.out.print("Press F when finished."); 
      System.out.println(); 
      do 
      { 
       System.out.print("Enter the integer price of the item: $"); 
       int i = in.nextInt(); 
       Prices.add(i); 
       System.out.println(); 
      } 
      while(in.hasNextInt()); 


      int totalPrice = processSale(Prices); 
      totalRegisterMoney = totalRegisterMoney + totalPrice; 
      System.out.print("Your total comes to $"); 
      System.out.println(totalPrice); 
     } 
     if(Choice == 2) 
     { 
      System.out.print("Press F when finished."); 
      System.out.println(); 
      do 
      { 
       System.out.print("Enter the price of the returned item: $"); 
       int j = in.nextInt(); 
       ReturnPrices.add(j); 
       System.out.println(); 
      } 
      while(in.hasNextInt()); 


      int returnTotal = processReturn(ReturnPrices); 
      if(returnTotal > totalRegisterMoney) 
      { 
       System.out.print("Sorry, there's not that much money in the register."); 
       System.out.println(); 
      } 
      else 
      { 
       totalRegisterMoney = totalRegisterMoney - returnTotal; 
      } 

      System.out.print("You've completed the return."); 
      System.out.println(); 
     } 
     if(Choice == 3) 
     { 
      viewBalance(totalRegisterMoney); 
     } 


    } 


    //END OF MAIN 

ответ

0

Если вы получили эту ошибку, когда делали это, тогда вы делали это нормально.

Проблема заключается в том, что после написания «F» для выхода из режима 1 возвращается цикла и пытается преобразовать, что «F» в

Choice = in.nextInt(); 

Это приводит к ошибке bucause в «F» не является номер. Вы должны были бы Погружают

in.next(); 

после

while(in.hasNextInt()); 

Это также будет происходить по варианту 2 в коде

+0

такого простого решения, но это сработало! ты! – iadd7249

Смежные вопросы