当循环不返回整个类时

huangapple 未分类评论54阅读模式
英文:

While loop not returning entire class

问题

我有两个while循环,一个内部循环和一个外部循环。我的内部循环的目的是仅在用户选择并选择退出后关闭特定部分的代码,外部循环的目的是在用户选择后重新运行整个函数,然后在选择退出后关闭。我这样做是为了可重用性。

> 然而,我遇到了3个问题。

  1. 当程序启动时,它不允许输入第一个输入值(Main类中的电影名称-String name),只允许输入后续的其他值,但当重新运行时,它允许输入第一个值。
  2. 在选择选项2的情况下,它不允许我在主类中输入完整名称(String fullName)。
  3. 在创建2部电影的情况下,不会为第二部电影创建新对象,它只会覆盖现有对象。

对于我的第三个问题,我猜想为了为每个创建的电影创建一个新的对象,应该创建一个循环?我不确定如何做到这一点。
我不知道为什么会出现第一个和第二个问题,但我猜想与内部和外部while循环有关。

我将尝试尽可能简洁地显示我的整个代码,但由于我要解决多个问题,代码可能仍然很长,提前道歉。
我有两个类

电影类

  1. 电影类代码

主类

  1. 主类代码
英文:

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.

  1. 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.
  2. In the case where I select option 2, it doesn't allow me to input the full name in the main class (String fullName).
  3. 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

  1. import java.util.*;
  2. import java.util.Arrays;
  3. public class Movie {
  4. private int id;
  5. private String name;
  6. private String description;
  7. private String[] genre;
  8. private String[] actors;
  9. private String[] language;
  10. private String countryOfOrigin;
  11. private Map<String, Integer> ratings;
  12. Movie() {
  13. }
  14. //Constructor
  15. public Movie(int id, String name, String description, String[] genre, String[] actors, String[] language, String countryOfOrigin, Map<String, Integer> ratings) {
  16. this.id = id;
  17. this.name = name;
  18. this.description = description;
  19. this.genre = genre;
  20. this.actors = actors;
  21. this.language = language;
  22. this.countryOfOrigin = countryOfOrigin;
  23. this.ratings = ratings;
  24. }
  25. //setters
  26. public void setid(int id) {
  27. this.id = id;
  28. }
  29. public void setname(String name) {
  30. this.name = name;
  31. }
  32. public void setDescription(String description) {
  33. this.description = description;
  34. }
  35. public void setGenre(String[] genre) {
  36. this.genre = genre;
  37. }
  38. public void setActors(String[] actors) {
  39. this.actors = actors;
  40. }
  41. public void setRatings(Map<String, Integer> ratings) {
  42. this.ratings = ratings;
  43. }
  44. //getters
  45. public int getId() {
  46. return id;
  47. }
  48. public String getName() {
  49. return name;
  50. }
  51. public String getDescription() {
  52. return description;
  53. }
  54. public String[] getGenre() {
  55. return genre;
  56. }
  57. @Override
  58. public String toString() {
  59. 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 +"";
  60. }
  61. }

Main class

  1. import java.util.HashMap;
  2. import java.util.Scanner;
  3. import java.util.Random;
  4. public class Main{
  5. // --- Private global scanner initialization --- //
  6. private static Scanner input = new Scanner(System.in);
  7. public static void main(String[] args) throws Exception{
  8. int choice;
  9. final int MAX = 999999999;
  10. boolean loopAgain = true;
  11. HashMap<String, Integer> mapRatings = new HashMap<>();
  12. while (loopAgain){
  13. System.out.println("\nMovie Voting App");
  14. System.out.println("\nChoose one of the follow options below");
  15. System.out.println("\t1. Create a poll");
  16. System.out.println("\t2. Vote on a poll");
  17. choice = input.nextInt();
  18. if (choice == 1) {
  19. while (loopAgain){
  20. Random rand = new Random();
  21. int id = rand.nextInt(MAX);
  22. System.out.println("\nCreate Poll");
  23. System.out.println("\nMovie Name: ");
  24. String name = input.nextLine();
  25. System.out.println("Movie Description: ");
  26. String description = input.nextLine();
  27. // --- Function for printing and storing genres in the movie --- //
  28. System.out.println("How many genre(s) is in this movie?: ");
  29. int num = Integer.parseInt(input.nextLine());
  30. String genre[] = new String[num];
  31. System.out.println("Enter the " + num + " genre(s) in the movie: ");
  32. for (int i = 0; i < genre.length; i++) {
  33. genre[i] = input.nextLine();
  34. }
  35. // --- Function for printing and storing actors into array --- //
  36. System.out.println("How many actor(s) star in this movie?: ");
  37. int num1 = Integer.parseInt(input.nextLine());
  38. String actors[] = new String[num1];
  39. System.out.println("Enter the " + num1 + " actor(s) starred now");
  40. for (int i = 0; i < actors.length; i++) {
  41. actors[i] = input.nextLine();
  42. }
  43. // --- Function for printing and storing the languages the movie is played in --- //
  44. System.out.println("How many language(s) does this movie play in?: ");
  45. int num2 = Integer.parseInt(input.nextLine());
  46. String language[] = new String[num2];
  47. System.out.println("Enter the " + num2 + " language(s) that this movie plays in: ");
  48. for (int i = 0; i < language.length; i++) {
  49. language[i] = input.nextLine();
  50. }
  51. System.out.println("Country of Origin: ");
  52. String countryOfOrigin = input.nextLine();
  53. System.out.println("Enter movie rating (PG OR NC): ");
  54. String rating = input.nextLine();
  55. System.out.println("Enter the rating age (13 OR 17): ");
  56. Integer ratingId = Integer.parseInt(input.nextLine());
  57. mapRatings.put(rating, ratingId);
  58. Movie movie1 = new Movie(id, name, description, genre, actors, language, countryOfOrigin, mapRatings);
  59. System.out.println("Enter another movie (y/n)?: ");
  60. String answer = input.nextLine();
  61. if (answer.equals("y") || answer.equals("Y")) {
  62. loopAgain = true;
  63. }
  64. else {
  65. System.out.println(movie1);
  66. System.out.println("Return the main menu (y/n? : ");
  67. String answer1 = input.nextLine();
  68. if (answer1.equals("y") || answer1.equals("Y")) {
  69. loopAgain = true;
  70. }
  71. else {
  72. break;
  73. }
  74. }
  75. }
  76. }
  77. else{
  78. System.out.println("Voting Poll");
  79. //Generating random ID
  80. Random rand = new Random();
  81. int id = rand.nextInt(MAX);
  82. System.out.println("\nEnter your full name:");
  83. String fullName = input.nextLine();
  84. //Creating object for class Person
  85. Person obj = new Person(fullName, id);
  86. System.out.println(obj);
  87. }
  88. }
  89. }
  90. }

huangapple
  • 本文由 发表于 2020年3月16日 05:05:21
  • 转载请务必保留本文链接:https://java.coder-hub.com/60697641.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定