나는 책상 주문 데이터를 지속적으로 받아들이고 길이가 36 인치 이상이고 적어도 하나의 서랍이있는 오크 책상에 대한 모든 관련 정보를 표시하는 프로그램을 만들어야합니다.루프가 한 번 이상 실행되지 않음
import java.util.*;
public class MangMaxB
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
char ans;
int orderNum=0, length=0, width=0, numDrawer=0, price=1000;
String name;
System.out.print("Do you wish to enter Oak " +
"desk order data? (y/n)");
ans = input.nextLine().charAt(0);
while (ans != 'n')
{
System.out.print("Enter customer name: ");
name=input.nextLine();
System.out.print("Enter order number: ");
orderNum=input.nextInt();
System.out.print("Enter length and width of Oak desk" +
" separated by a space: ");
length = input.nextInt();
width = input.nextInt();
System.out.print("Enter number of drawer/s: ");
numDrawer=input.nextInt();
if ((length>36)&&(numDrawer>=1))
{
if ((length*width)>750)
{
price+= 250;
}
price+= (numDrawer*100);
price+= 300;
System.out.println("\nOak desk order information:\n"
+ "Order number: " + orderNum + "\n"
+ "Customer name: " + name + "\n"
+ "Length: " + length + ", width: "
+ width + ", surface: " + (length*width)
+ "\n" + "Number of drawer/s: " + numDrawer
+ "\nPrice of the desk is P " + price);
}
else
{
System.out.println("\nOak desk order isn't over 36 " +
"inches long and doesn't have a drawer");
}
System.out.print("Any more items? (y/n) ");
ans = input.nextLine().charAt(0);
}
}
}
데이터를 입력하고 표시 할 수 있지만 루프이므로 두 번째 시도에서 작동하지 않았습니다. "문자열 색인이 범위를 벗어 : controlStruc.MangMaxB.main에서 java.lang.String.charAt (알 수없는 소스) 에서 0 java.lang.StringIndexOutOfBoundsException"
그것은 "스레드에서 예외"주요 말한다
어떻게 해결할 수 있습니까?
왜'String' 객체를 다루지 않는가? 'String'에서'char'을 얻는 데 진짜 포인트가 없습니다. 어쨌든, 그것은 그것이 작동해야하는 것처럼 나에게 보인다, 당신은 그것이 묻힐 때 당신이 문자를 입력 했습니까? 을 입력했다면을 입력하면'String'의 위치 0에'char'가 없습니다. – Quetzalcoatl
글쎄, 그것은 마치'input.nextLine()'이 루프의 끝에있는 빈 문자열 인 것처럼 보입니다. –
@SamIam 그것은 왜 여기에 진짜 질문입니까? * 빈 문자열입니다. – berry120