Java petle do while

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.

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("");
	}

}
0

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



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 : " );
		}
	}

}
1

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

 ppl = myinput.nextInt(); 

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

 i potem sumuj po kolejnym wczytaniu: <code class="java">ticketCount += ppl 
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.

0

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

		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);
		}
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.

		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);

0
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 :////

0

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

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);
		}
	}

}
 
0

Wielkie dzieki Olek o to wlasnie mi chodzilo.

Pozdrawiam

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

1 użytkowników online, w tym zalogowanych: 0, gości: 1