英文:
While loop not returning entire class
问题
我有两个while循环,一个内部循环和一个外部循环。我的内部循环的目的是仅在用户选择并选择退出后关闭特定部分的代码,外部循环的目的是在用户选择后重新运行整个函数,然后在选择退出后关闭。我这样做是为了可重用性。
> 然而,我遇到了3个问题。
- 当程序启动时,它不允许输入第一个输入值(
Main类中的电影名称-String name
),只允许输入后续的其他值,但当重新运行时,它允许输入第一个值。 - 在选择选项2的情况下,它不允许我在主类中输入完整名称(
String fullName
)。 - 在创建2部电影的情况下,不会为第二部电影创建新对象,它只会覆盖现有对象。
对于我的第三个问题,我猜想为了为每个创建的电影创建一个新的对象,应该创建一个循环?我不确定如何做到这一点。
我不知道为什么会出现第一个和第二个问题,但我猜想与内部和外部while循环有关。
我将尝试尽可能简洁地显示我的整个代码,但由于我要解决多个问题,代码可能仍然很长,提前道歉。
我有两个类
电影类
(电影类代码)
主类
(主类代码)
英文:
I have two while loops, one inner and one outer. The intent of my inner one is to only re-rerun a specific aspect of my code should the user choose to and then close said function after they opt out, and the intent for the outer while loop is to re-run the entire function should the user choose to and then close after they opt out. I am doing this for reusability sake.
> However, I am running into 3 issues.
- When the program starts, it doesn't allow the first input value to be entered (
Movie name in Main class - String name
) only the other values that follows, but when it re-reruns, then it allows for the first value to be entered. - In the case where I select option 2, it doesn't allow me to input the full name in the main class (
String fullName
). - In the case where I create 2 movies, a new object isn't created for the second movie, it just overwrites the existing one.
For my third issue, I'm guessing that in order to create a new obj for each movie created, a loop should be created? I'm not sure how to go about doing that.
I have no idea why I am facing the 1st and 2nd problems but I am guessing it has something to do with the inner and outer while loops.
I'll try to show my entire code as condense as I can but it may still be lengthy since I am addressing multiple issues, I do apologize in advance.
I have 2 classes
Movie class
import java.util.*;
import java.util.Arrays;
public class Movie {
private int id;
private String name;
private String description;
private String[] genre;
private String[] actors;
private String[] language;
private String countryOfOrigin;
private Map<String, Integer> ratings;
Movie() {
}
//Constructor
public Movie(int id, String name, String description, String[] genre, String[] actors, String[] language, String countryOfOrigin, Map<String, Integer> ratings) {
this.id = id;
this.name = name;
this.description = description;
this.genre = genre;
this.actors = actors;
this.language = language;
this.countryOfOrigin = countryOfOrigin;
this.ratings = ratings;
}
//setters
public void setid(int id) {
this.id = id;
}
public void setname(String name) {
this.name = name;
}
public void setDescription(String description) {
this.description = description;
}
public void setGenre(String[] genre) {
this.genre = genre;
}
public void setActors(String[] actors) {
this.actors = actors;
}
public void setRatings(Map<String, Integer> ratings) {
this.ratings = ratings;
}
//getters
public int getId() {
return id;
}
public String getName() {
return name;
}
public String getDescription() {
return description;
}
public String[] getGenre() {
return genre;
}
@Override
public String toString() {
return "\nMovie Id: " + id + " \nMovie Name: " + name + "\nMovie Description: " + description + "\nMovie Genre(s): " + Arrays.toString(genre) + "\nMovie Actor(s): " + Arrays.toString(actors) + "\nMovie Language(s): " + Arrays.toString(language) + "\nCountry of Origin: " + countryOfOrigin + "\nRatings: " + ratings +"";
}
}
Main class
import java.util.HashMap;
import java.util.Scanner;
import java.util.Random;
public class Main{
// --- Private global scanner initialization --- //
private static Scanner input = new Scanner(System.in);
public static void main(String[] args) throws Exception{
int choice;
final int MAX = 999999999;
boolean loopAgain = true;
HashMap<String, Integer> mapRatings = new HashMap<>();
while (loopAgain){
System.out.println("\nMovie Voting App");
System.out.println("\nChoose one of the follow options below");
System.out.println("\t1. Create a poll");
System.out.println("\t2. Vote on a poll");
choice = input.nextInt();
if (choice == 1) {
while (loopAgain){
Random rand = new Random();
int id = rand.nextInt(MAX);
System.out.println("\nCreate Poll");
System.out.println("\nMovie Name: ");
String name = input.nextLine();
System.out.println("Movie Description: ");
String description = input.nextLine();
// --- Function for printing and storing genres in the movie --- //
System.out.println("How many genre(s) is in this movie?: ");
int num = Integer.parseInt(input.nextLine());
String genre[] = new String[num];
System.out.println("Enter the " + num + " genre(s) in the movie: ");
for (int i = 0; i < genre.length; i++) {
genre[i] = input.nextLine();
}
// --- Function for printing and storing actors into array --- //
System.out.println("How many actor(s) star in this movie?: ");
int num1 = Integer.parseInt(input.nextLine());
String actors[] = new String[num1];
System.out.println("Enter the " + num1 + " actor(s) starred now");
for (int i = 0; i < actors.length; i++) {
actors[i] = input.nextLine();
}
// --- Function for printing and storing the languages the movie is played in --- //
System.out.println("How many language(s) does this movie play in?: ");
int num2 = Integer.parseInt(input.nextLine());
String language[] = new String[num2];
System.out.println("Enter the " + num2 + " language(s) that this movie plays in: ");
for (int i = 0; i < language.length; i++) {
language[i] = input.nextLine();
}
System.out.println("Country of Origin: ");
String countryOfOrigin = input.nextLine();
System.out.println("Enter movie rating (PG OR NC): ");
String rating = input.nextLine();
System.out.println("Enter the rating age (13 OR 17): ");
Integer ratingId = Integer.parseInt(input.nextLine());
mapRatings.put(rating, ratingId);
Movie movie1 = new Movie(id, name, description, genre, actors, language, countryOfOrigin, mapRatings);
System.out.println("Enter another movie (y/n)?: ");
String answer = input.nextLine();
if (answer.equals("y") || answer.equals("Y")) {
loopAgain = true;
}
else {
System.out.println(movie1);
System.out.println("Return the main menu (y/n? : ");
String answer1 = input.nextLine();
if (answer1.equals("y") || answer1.equals("Y")) {
loopAgain = true;
}
else {
break;
}
}
}
}
else{
System.out.println("Voting Poll");
//Generating random ID
Random rand = new Random();
int id = rand.nextInt(MAX);
System.out.println("\nEnter your full name:");
String fullName = input.nextLine();
//Creating object for class Person
Person obj = new Person(fullName, id);
System.out.println(obj);
}
}
}
}
专注分享java语言的经验与见解,让所有开发者获益!
评论