eine Ausgabe zum Ausgeben einen String aus einem Array-Index für einen zufälligen Satz Ausgang. Die hashmap ist in Ordnung, wie viele von jeder Zeichenfolge im Array, aber sobald es alle Informationen hat geben Sie eine int-Anzeige der Elemente. Ziel ist es, den Benutzer Formularelemente auswählen zu lassen, danach Optionen für Subjekt - Verb - Adjektiv - Objekt - Adverb auszuwählen und dann eine zufällige Ausgabe zu machen. auch aus irgendeinem Grund, wenn es das Adverb getroffen hat, ist es die Benutzereingabeinfo vor angegeben.ein Problem aus Mit einem String aus einem Array-Index für eine zufällige Ausgabe setzt
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package shoutbox;
import java.util.HashMap;
import java.util.Random;
import java.util.Scanner;
/**
*
* @author alden
*/
public class Shoutbox {
/**
*
*/
public int t;
public int o;
public int r;
public int q;
public int p;
public String subjectArray[]=new String[t]; // Intilized array by the size of int y
public String verbArray[]=new String[o];
public String adjectiveArray[]=new String[r];
public String objectArray[]=new String[q];
public String adverbArray[]=new String[p];
public Shoutbox(){
}
public static void main(String[] args) {
// TODO code application logic here
Boolean repeat = true;
System.out.println(" Please select a option");
HashMap <Integer, String> map = new HashMap<>(); // starts the hashmap
map.put(0,"Are you new to programming");
map.put(1, "I think java is a good start");
map.put(2, "It is easy to learn");
map.put(3, "You tube is a great resource");
map.put(4,"Most people start with hello world");
map.put(5,"After this you learn Scanners and getter and setters");
map.put(6,"Next is encapsulation");
map.put(7,"After this is loops");
map.put(8,"Once you get loops work on arrays");
map.put(9,"Next you should look at objects");
map.entrySet().forEach((entry) -> {
// for loop for the display of has map
int key = entry.getKey();
String value = entry.getValue();
System.out.println(key + " : " + value);
});
char y;
while (repeat = true){ // gives options for y or n
System.out.println("Please enter a value");
Scanner in = new Scanner(System.in);
int n = in.nextInt();
String s = map.get(n);
System.out.println(s);
System.out.println("Would you like to enter another value. y/n");
y = in.next().charAt(0);
switch (y) {
case 'y':
repeat = true;
break;
case 'Y':
repeat = true;
break;
default:
repeat = false;
break;
}
break;
}
String input1;
boolean loop = false;
Scanner in = new Scanner(System.in);
System.out.printf("How many subjects are there?");
int t = in.nextInt();
String subjectArray[]=new String[t];
System.out.printf("How many verbs are there?");
int o = in.nextInt();
String verbArray[]=new String[o];
System.out.printf("How many adjective are there?");
int q = in.nextInt();
String adjectiveArray[]=new String[q];
System.out.printf("How many adjective are there?");
int r = in.nextInt();
String objectArray[]=new String[r];
System.out.printf("How many adverbs are there?");
int p = in.nextInt();
String adverbArray[]=new String[p];
Scanner input = new Scanner(System.in); // aanother scanner for more input
for(int subjectCount=0;subjectCount<t;subjectCount++){
subjectArray[subjectCount]= input.next();
int tempSub= subjectCount+1;
System.out.printf("Subject "+tempSub);
// Assigned users value to array
}
int subjectIndex;
Random randt = new Random();
subjectIndex = randt.nextInt(+1);//for loop
// random word
//Stores and finds random subject
//System.out.println(subjectArray[subjectIndex]);
for(int verbCount=0;verbCount<o;verbCount++){
verbArray[verbCount]= input.next(); // Assigned users value to array
int tempVerb= verbCount+1;
System.out.printf("verb "+tempVerb);
}
int verbIndex;
Random rando = new Random();
verbIndex = rando.nextInt(o); // random word
//
for(int adjectiveCount=0;adjectiveCount<r;adjectiveCount++){
adjectiveArray[adjectiveCount]= input.next(); // Assigned users value to array
int tempAdj= adjectiveCount+1;
System.out.printf("Adjective " + tempAdj);
}
int adjectiveIndex;
Random randq = new Random();
adjectiveIndex = randq.nextInt(r); // random word
//
for(int objectCount=0;objectCount<q;objectCount++){
objectArray[objectCount]= input.next(); // Assigned users value to array
int tempObj= objectCount+1;
System.out.printf("Object "+ tempObj);
}
int objectIndex;
Random randr = new Random();
objectIndex = randr.nextInt(q); // random word
//
for(int adverbCount=0;adverbCount<p;adverbCount++){
adverbArray[adverbCount]= input.next(); // Assigned users value to array
int tempAd= adverbCount+1;
System.out.printf("Adverb "+tempAd);
}
int adverbIndex;
Random randp = new Random();
adverbIndex = randp.nextInt(p);
input1 = (subjectIndex + " " + verbIndex +" "+ adjectiveIndex +" "+ objectIndex +" "+ adverbIndex); // adds all items togather for loop
while(loop = true){
System.out.println(input1);
break;
}
}}
Machen Sie keine neuen Scanner überall. Machen Sie einen "Scanner" am Anfang des Programms und verwenden Sie ihn dort, wo Sie Eingaben benötigen. Das sollte einige Ihrer I/O-Probleme behandeln. –
Ich änderte es läuft etwas schneller immer noch versuchen, Strings aus den Arrays zu sammeln, um einen zufälligen Satz auszugeben gibt es eine bessere Möglichkeit, es zu tun, dann mache ich? –