如何在for循环中调用变量?

时间:2019-03-21 18:26:46

标签: java scope

我似乎无法访问在创建的3个循环中定义的randElement1randElement2randElement3

我的程序创建一个字符串,该字符串具有从列表中选择的3个随机元素。

我知道这是一个简单的问题,我确实进行了查找。我已经阅读了有关Scopes的所有文章,但是甚至找不到针对我的特定编码问题的解决方案。谢谢。

import java.util.Scanner;
import java.util.ArrayList;
import java.util.Random;

public class Main {
  public static void main(String[] args) 
{
    Scanner userInput = new 
    Scanner(System.in);
    Random rand = new Random();

//Insult Bank
    String[] shakeInsults3 = new String[].{"scut", "apple-john", "baggage", "barnacle", "bladder", "boar-pig", "bugbear", "codpiece","flap-dragon", "giglet", "horn-beast","jolthead","wagtail","scut","pignut","vassal","moldwarp","lout","lewdster","measle","minnow","pumpion","ratsbane","varlet","maggot-pie","harpy"};

    String[] shakeInsults1 = new String[]{"artless", "bawdy", "beslubbering","bootless", "churlish", "cockered", "clouted", "craven", "currish", "dankish", "dissembling", "droning", "errant", "fawning", "fobbing", "froward", "frothy", "gleeking", "goatish", "gozbellied", "impertinent", "infectious", "jarring", "loggerheaded", "lumpish", "mammering", "mangled", "mewling", "paunchy", "pribbling", "puking", "puny", "quailing", "rank", "reeky", "roguish", "ruttish", "saucy", "spleeny", "spongy", "surly", "tottering", "unnuzzled", "vain", "venomed", "villainous", "warped", "wayward", "weedy", "yeasty"};

    String[] shakeInsults2 = new String[]{"base-court", "bat-fowling", "beef-witted", "beetle-headed", "boil-brained", "clapper-clawed", "clay-brained", "common-kissing", "crooked=pated", "dismal-dreaming", "dizzy-eyed","elf skinned", "fat=kidneyed", "fen-sucked", "fool-born", "half-faced", "hell-hated", "onion-eyed", "swag-bellied","reeling-ripe","rude-growing","ill-nurtured","guts-griping","shard-borne"};

//declaring
    int intRandomWord1 = shakeInsults1.length;
    int intRandomWord2 = shakeInsults2.length;
    int intRandomWord3 = shakeInsults3.length;


    for (int s = 0; s < 1; s++) {
      for (int i = 0; i < intRandomWord1; i++) {
          int randIndex1 = (int)(Math.random() * shakeInsults1.length);
          String randElement1 = shakeInsults1[randIndex1];       
  }

      for (int i = 0; i < intRandomWord2; i++) {
          int randIndex2 = (int)(Math.random() * shakeInsults2.length); 
          String randElement2 = shakeInsults2[randIndex2];

      }

      for (int i = 0; i < intRandomWord3; i++) {
          int randIndex3 = (int)(Math.random() * shakeInsults3.length); 
          String randElement3 = shakeInsults3[randIndex3];
      }
    }
    System.out.println("Thou ", + randElement1, ", " + randElement2 + ", " + randElement3 + ".");


  }


}

5 个答案:

答案 0 :(得分:2)

在for循环外启动它们

答案 1 :(得分:2)

我认为这是您要尝试做的(尽管抱歉,我现在无法在我的机器上对其进行测试。)请注意,Random只是一个伪随机数生成器,当真正的随机性时不应该使用是必须的。在循环内部声明的任何变量在循环外部将不存在。每次循环都会为randElement1、2和3生成一个新字符串。

   // This is so you don't get index out of bounds errors if the lists are different lengths.
   // You could combine these into one line with nested Math.min calls.
   // I made it two lines to be easier for you to read.
   int minListLength = Math.min(shakeInsults1.length, shakeInsults2.length);
   minListLength = Math.min(minListLength, shakeInsults3.length);

   for (int s = 0; s < minListLength; s++) {

          String randElement1 = shakeInsults1[rand.nextInt(shakeInsults1.length)];       

          String randElement2 = shakeInsults2[rand.nextInt(shakeInsults2.length)];

          String randElement3 = shakeInsults3[rand.nextInt(shakeInsults3.length)];

          System.out.println("Thou ", + randElement1, ", " + randElement2 + ", " + randElement3 + ".");

  }

答案 2 :(得分:1)

您不能在循环内初始化变量并在外部使用它,如果要在外部使用它,则必须在要使用它的方法中声明它。 <3

答案 3 :(得分:0)

由于您声明了 for循环中的变量,因此它们的范围仅限于for循环中。要在for循环外访问它们,必须在for循环外声明它们。您可以尝试类似的操作:

int intRandomWord1 = shakeInsults1.length;
  int intRandomWord2 = shakeInsults2.length;
  int intRandomWord3 = shakeInsults3.length;

  String randElement1;
  String randElement2;
  String randElement3;


  for (int i = 0; i < intRandomWord1; i++) {
      int randIndex1 = (int)(Math.random() * shakeInsults1.length);
      randElement1 = shakeInsults1[randIndex1];       
  }

  for (int i = 0; i < intRandomWord2; i++) {
      int randIndex2 = (int)(Math.random() * shakeInsults2.length); 
      randElement2 = shakeInsults2[randIndex2];
  }

  for (int i = 0; i < intRandomWord3; i++) {
      int randIndex3 = (int)(Math.random() * shakeInsults3.length); 
      randElement3 = shakeInsults3[randIndex3];
}

您实际上不需要此解决方案的循环。要对此程序进行无for循环编程,请尝试类似于Bakon Jarser建议的内容。

答案 4 :(得分:0)

所以尝试一下,让我知道如何进行。

int randIndex1 = 0;
String randElement1 = "";

int randIndex2 = 0;
String randElement2 = "";

int randIndex3 = 0;
String randElement3 = "";

for (int s = 0; s < 1; s++) 
{
  for (int i = 0; i < intRandomWord1; i++) 
  {
      randIndex1 = (int)(Math.random() * shakeInsults1.length);
      randElement1 = shakeInsults1[randIndex1];       
  }
  for (int i = 0; i < intRandomWord2; i++) 
  {
      randIndex2 = (int)(Math.random() * shakeInsults2.length); 
      randElement2 = shakeInsults2[randIndex2];

  }

  for (int i = 0; i < intRandomWord3; i++) 
  {
      randIndex3 = (int)(Math.random() * shakeInsults3.length); 
      randElement3 = shakeInsults3[randIndex3];
  }
}

我希望能有所帮助。我不知道您是否要添加任何值或返回任何内容。如果有问题请发表评论。