Java petle do while

I3
  • Rejestracja:ponad 9 lat
  • Ostatnio:ponad 7 lat
  • Postów:42
0

Witam wszystkich

Jezeli byl juz taki temat to przeprawszam za spam ale potrzebuje pomocy

Mam zrobic program ktory bedzie zczytywal dane od uzytkownika drobne obliczenia a na koncu pytal sie czy powtorzyc Y na tak czy zakonczyc N wiem ze musze uzyc do while ale nie wiem jak dokladnie to zastosowac, mozg mi zardzewial
Oto moj program jak by mogl mi ktos pomoc bylbym bardzo wdzieczny.

Kopiuj
import java.util.Scanner; //importing Scanner

class Ass1 	//Program name
{
	public static void main(String [] args) //class type
	{
	String name,show;
	int ppl,ticket;
	double sum;
	ticket = 25;



		Scanner myinput = new Scanner(System.in);
		System.out.println("Please enter your name: ");
		name = myinput.next(); 
		System.out.println("Please enter name of the show: "); 
		show = myinput.next(); 
		System.out.println("Please enter number of tickets you would like to buy: "); 
		ppl = myinput.nextInt(); 
		System.out.println("");
		System.out.println("***********************************************");
		System.out.println("Your name is: " + name);
		System.out.println("The name of the show is : " +show );
		System.out.println("The number of pepole : " +ppl );




			if (ppl >6)
			{
			sum = (ticket*ppl)-(ticket*ppl*0.1);
			System.out.println("Total price for tickets is : " +sum); 
			}
			else
				if (ppl <=6)
				{
				sum = ticket*ppl;
				System.out.println("Total price for tickets is : " +sum); 
				}
		System.out.println("");
		System.out.println("***********************************************");
		System.out.println("Would you like to buy another ticket Y or N: ");
		System.out.println("");
	}

}
edytowany 1x, ostatnio: bogdans
bogdans
!Wstawiaj kod w znaczniki &lt;code=java&gt;&lt;/code&gt;
I3
  • Rejestracja:ponad 9 lat
  • Ostatnio:ponad 7 lat
  • Postów:42
0

ok mam juz dzialajaca petle ale nie wiem jak zrobic zeby liczyl ilosc biletow w petli i zeby petlil tylko na litere Y

Kopiuj


import java.util.Scanner; //importing Scanner

class Ass1B00092367 	//Program name
{
	public static void main(String [] args) //class type
	{
	String name,show;
	int ppl,ticket;
	double sum;
	ticket = 25;
	char end = 'Y';

		do
		{
		Scanner myinput = new Scanner(System.in);
		System.out.println("Please enter your name: "); // asks user to enter number of apples to buy
		name = myinput.next(); // scans imput
		System.out.println("Please enter name of the show: "); // asks user to enter number of apples to buy
		show = myinput.next(); // scans imput
		System.out.println("Please enter number of tickets you would like to buy: "); // asks user to enter number of apples to buy
		ppl = myinput.nextInt(); // scans imput
		System.out.println("");
		System.out.println("***********************************************");
		System.out.println("Your name is: " + name);
		System.out.println("The name of the show is : " +show );
		System.out.println("The number of pepole : " +ppl );




			if (ppl >6)
			{
			sum = (ticket*ppl)-(ticket*ppl*0.1);
			System.out.println("Total price for tickets is : " +sum); // asks user to enter number of apples to buy
			}
			else
				if (ppl <=6)
				{
				sum = ticket*ppl;
				System.out.println("Total price for tickets is : " +sum); // asks user to enter number of apples to buy
				}

		System.out.println("");
		System.out.println("***********************************************");
		System.out.println("Would you like to buy another ticket Y or N: ");
		end = myinput.next().charAt(0);
		System.out.println("");
		}
		while(end !='N');
		{

		System.out.println("Total nubmer of ticket processed is : " );
		}
	}

}
edytowany 1x, ostatnio: Ic3m4n
O1
  • Rejestracja:ponad 14 lat
  • Ostatnio:około 19 godzin
1

Utwórz zmienną, która po każdym

Kopiuj
 ppl = myinput.nextInt(); 

będzie sumować wszystkie bilety, np. zrób zmienną i zainicializuj ją przed pętlą na 0int ticketCount = 0

Kopiuj
 i potem sumuj po kolejnym wczytaniu: <code class="java">ticketCount += ppl 
edytowany 1x, ostatnio: olek1
I3
  • Rejestracja:ponad 9 lat
  • Ostatnio:ponad 7 lat
  • Postów:42
0

Wielkie dzieki Olek mam tylko maly probem jak zrobic zeby petla dzialala na Y i N. Na N zakancza ale chce zeby tylko petlil jak naciskam Y a teraz petli na kazda litere lub cyfre. Nie wiem jaki warunek sporzadzic.

O1
  • Rejestracja:ponad 14 lat
  • Ostatnio:około 19 godzin
0

Można zrobić coś takiego, chociaż nie jestem pewien czy jest to najlepsze rozwiązanie i nie można zrobić tego lepiej.

Kopiuj
		System.out.println("Would you like to buy another ticket Y or N: ");
		while (end != 'N' && end != 'Y') {
			System.out.println("Only accept Y or N");
			end = myinput.next().charAt(0);
		}
edytowany 2x, ostatnio: olek1
I3
  • Rejestracja:ponad 9 lat
  • Ostatnio:ponad 7 lat
  • Postów:42
0
olek1 napisał(a):

Można zrobić coś takiego, chociaż nie jestem pewien czy jest to najlepsze rozwiązanie i nie można zrobić tego lepiej.

Kopiuj
		System.out.println("Would you like to buy another ticket Y or N: ");
		while (end != 'N' && end != 'Y') {
			System.out.println("Only accept Y or N");
			end = myinput.next().charAt(0);
		}

Problem jest taki po przeniesieniu end = myinput.next pod while wyrzuca blad error: cannot find symbol end = myinput.next().charAt(0);

O1
Gdzie Ty ten end przeniosłeś i po co?
I3
  • Rejestracja:ponad 9 lat
  • Ostatnio:ponad 7 lat
  • Postów:42
0
Kopiuj
while(end !='N' && end != 'Y');
			{
			System.out.println("Total nubmer of ticket processed is : " + ticketcount );
			System.out.println("");
			}

Tak to teraz wyglada i nadal nie dziala jak naciskam N wychodzi jak naciskam co kolwiek nadal petli :////

O1
  • Rejestracja:ponad 14 lat
  • Ostatnio:około 19 godzin
0

Działa jak powinno, przeanalizuje sobie sam gdzie należało to dodać.

Kopiuj
import java.util.Scanner;

public class Main {

	public static void main(String[] args) // class type
	{
		String name, show;
		int ppl, ticket;
		double sum;
		ticket = 25;
		int ticketCount = 0;
		char end = 'Y';

		do {
			Scanner myinput = new Scanner(System.in);
			System.out.println("Please enter your name: "); // asks user to
															// enter number of
															// apples to buy
			name = myinput.next(); // scans imput
			System.out.println("Please enter name of the show: "); // asks user
																	// to enter
																	// number of
																	// apples to
																	// buy
			show = myinput.next(); // scans imput
			System.out.println("Please enter number of tickets you would like to buy: "); // asks
																							// user
																							// to
																							// enter
																							// number
																							// of
																							// apples
																							// to
																							// buy
			ppl = myinput.nextInt(); // scans imput
			ticketCount += ppl;
			System.out.println("");
			System.out.println("***********************************************");
			System.out.println("Your name is: " + name);
			System.out.println("The name of the show is : " + show);
			System.out.println("The number of pepole : " + ppl);

			if (ppl > 6) {
				sum = (ticket * ppl) - (ticket * ppl * 0.1);
				System.out.println("Total price for tickets is : " + sum); // asks
																			// user
																			// to
																			// enter
																			// number
																			// of
																			// apples
																			// to
																			// buy
			} else if (ppl <= 6) {
				sum = ticket * ppl;
				System.out.println("Total price for tickets is : " + sum); // asks
																			// user
																			// to
																			// enter
																			// number
																			// of
																			// apples
																			// to
																			// buy
			}

			System.out.println("");
			System.out.println("***********************************************");
			System.out.println("Would you like to buy another ticket Y or N: ");
			end = myinput.next().charAt(0);
			while (end != 'N' && end != 'Y') {
				System.out.println("Only accept Y or N");
				end = myinput.next().charAt(0);
			}
			System.out.println("");
		} while (end != 'N');
		{

			System.out.println("Total nubmer of ticket processed is : " + ticketCount);
		}
	}

}
 
edytowany 1x, ostatnio: olek1
Patryk27
Te komentarze w kodzie są zbędne.
I3
  • Rejestracja:ponad 9 lat
  • Ostatnio:ponad 7 lat
  • Postów:42
0

Wielkie dzieki Olek o to wlasnie mi chodzilo.

Pozdrawiam

ps. Jezeli chodzi o komentarze to kopiwalem to z wczesniejszego programu zaraz je poprawie.

Zarejestruj się i dołącz do największej społeczności programistów w Polsce.

Otrzymaj wsparcie, dziel się wiedzą i rozwijaj swoje umiejętności z najlepszymi.