while switch loop for explicacion entre ejercicios diferencia java loops while-loop do-while

switch - ejercicios do while java netbeans



Java hacer mientras regresa al principio de la aplicaciĆ³n (2)

Esta pregunta es un seguimiento de este enlace Java SE doy problemas de ciclo

Esta es una aplicación que solicita el nombre de una habitación y luego una segunda solicitud para consultar sus dimensiones para realizar algunos cálculos. Inmediatamente después de que se haya hecho todo, la aplicación debería poder volver al primer mensaje que le solicita el nombre de la habitación, etc.

class Room { private String name; private double length; private double width; private double height; public Room(String r, double l, double w, double h) { name = r; length = l; width = w; height = h; } public String getName() { return name; } public double getLength() { return length; } public double getWidth() { return width; } public double getHeight() { return height; } public double getArea() { double area = length*width; return area; } public double getVolume() { double volume = length*width*height; return volume; } public void setName(String s) { name = s; } }

Y mi principal método para ejecutar la aplicación.

import java.util.Scanner; class TestRoom { public static void main(String [] args) { Scanner userInput = new Scanner(System.in); System.out.println("Please enter the name of room"); String roomName = userInput.next(); while(!(roomName.equals("quit"))) { Room r; do { Scanner dimensionsInput = new Scanner(System.in); System.out.print("Please enter the dimensions of the room, length, width and height accordingly"); double roomLength = dimensionsInput.nextDouble(); double roomWidth = dimensionsInput.nextDouble(); double roomHeight = dimensionsInput.nextDouble(); r = new Room(roomName, roomLength, roomWidth, roomHeight); } while (r.getLength() == 0 || r.getWidth() == 0 || r.getHeight() == 0); System.out.println("The name of room: " + r.getName() + " Dimensions: L= " + r.getLength() + " X " + "W= " + r.getWidth() + " X " + "H= " + r.getHeight()); System.out.println("Area of room= " + r.getArea()); System.out.println("Volume of room= " + r.getVolume()); } userInput.close(); } }

En este momento, la aplicación solo vuelve al segundo aviso en lugar de al primero, así que ¿podrían ayudarme en esto? Gracias de antemano otra vez chicos!


No necesita declarar dos escáneres. Obviamente, también debiste incluir el primer aviso en el bucle while externo.

Código "fijo":

import java.util.Scanner; class TestRoom { public static void main(String[] args) { Scanner userInput = new Scanner(System.in); String roomName = ""; while (!(roomName.equals("quit"))) { System.out.println("Please enter the name of room"); roomName = userInput.next(); Room r; do { System.out .print("Please enter the dimensions of the room, length, width and height accordingly"); double roomLength = userInput.nextDouble(); double roomWidth = userInput.nextDouble(); double roomHeight = userInput.nextDouble(); r = new Room(roomName, roomLength, roomWidth, roomHeight); } while (r.getLength() == 0 || r.getWidth() == 0 || r.getHeight() == 0); System.out.println("The name of room: " + r.getName() + " Dimensions: L= " + r.getLength() + " X " + "W= " + r.getWidth() + " X " + "H= " + r.getHeight()); System.out.println("Area of room= " + r.getArea()); System.out.println("Volume of room= " + r.getVolume()); } userInput.close(); } }


Prueba esto:

class TestRoom { static Scanner userInput = new Scanner(System.in); static Scanner dimensionsInput = new Scanner(System.in); public static void main(String[] args) { while(true) { prompt(); } } private static void prompt() { System.out.println("Please enter the name of room"); String roomName = userInput.next(); if (roomName.equals("quit")){ userInput.close(); dimensionsInput.close(); System.exit(0); } Room r; do { System.out .print("Please enter the dimensions of the room, length, width and height accordingly"); double roomLength = dimensionsInput.nextDouble(); double roomWidth = dimensionsInput.nextDouble(); double roomHeight = dimensionsInput.nextDouble(); r = new Room(roomName, roomLength, roomWidth, roomHeight); } while (r.getLength() == 0 || r.getWidth() == 0 || r.getHeight() == 0); System.out.println("The name of room: " + r.getName() + " Dimensions: L= " + r.getLength() + " X " + "W= " + r.getWidth() + " X " + "H= " + r.getHeight()); System.out.println("Area of room= " + r.getArea()); System.out.println("Volume of room= " + r.getVolume()); } }