这是我的主要工作正常。我试图让它运行,直到用户输入“。”然后它会说再见。我尝试了一个while循环,但它给了我一个错误。我想就如何循环它提出一些建议。我认为这是时期。我尝试了条件== true。
public class Main {
public static void main(String[] args) throws Exception
{
System.out.printf("Welcome to name database\n\n");
NameHandler nameHandler = new NameHandler();
NamePrinter namePrinter = new NamePrinter();
SecureRandom random = new SecureRandom();
// boolean condition = true;
List<Name> name
= nameHandler.findName();
namePrinter.print(name);
Scanner scanner = new Scanner(System.in);
Scanner sc=new Scanner(System.in);
String str=sc.next()
while(!str.equals("."))
{
System.out.printf("\nENTER DATA FOR NEW NAME\n");
Integer id = random.nextInt(1000);
System.out.printf("FIRST_NAME: ");
String fname = scanner.nextLine();
System.out.printf("LAST_NAME: ");
String lname = scanner.nextLine();
Name newName = new Name();
newName.setFname(fname);
newName.setId(id);
newName.setLname(lname);
int insertCount = nameHandler.insert(newName);
System.out.printf("%nInsert count: %d%n", insertCount);
namePrinter.print(
nameHandler.findName());
System.out.printf("\nUPDATE NAME\n");
System.out.printf("ID: ");
id = Integer.parseInt(scanner.nextLine());
System.out.printf("FIRST_NAME: ");
fname = scanner.nextLine();
System.out.printf("LAST_NAME: ");
lname = scanner.nextLine();
int updateCount = nameHandler.update(id, fname, lname);
System.out.printf("%nUpdate count: %d%n", updateCount);
namePrinter.print(
nameHandler.findName()
);
System.out.printf("\nDELETE A NAME\n");
System.out.printf("ID: ");
id = Integer.parseInt(scanner.nextLine());
int deleteCount = nameHandler.delete(id);
System.out.printf("%nDelete count: %d%n", deleteCount);
namePrinter.print(
nameHandler.findName()
);}
System.out.printf("\nGood bye!\n");
}
答案 0 :(得分:1)
这只是您查询的示例代码:
Scanner sc=new Scanner(System.in);
String str=sc.next()
while(!str.equals("."))
{
//code that needs to loop
}