Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

188
Visualizações
Can you use the value of a variable to select an already defined variable with that same name?

I'm trying to try something out for my AP statistics class where I need to randomly select five words from the lyrics of a song and calculate the average length of those strings. This is what I have so far: (There's 297 lyrics but I don't want to type all of those out if it won't work)

        String n001 = "I";
        String n002 = "look";
        String n003 = "and";
        String n004 = "stare";
        String n005 = "so";
        
        Random bob = new Random();
     
        String num_1 = String.format("%03d", bob.nextInt(298)); 
        System.out.println(num_1);
        String num_2 = String.format("%03d", bob.nextInt(298)); 
        System.out.println(num_2);
        String num_3 = String.format("%03d", bob.nextInt(298)); 
        System.out.println(num_3);
        String num_4 = String.format("%03d", bob.nextInt(298)); 
        System.out.println(num_4);
        String num_5 = String.format("%03d", bob.nextInt(298)); 
        System.out.println(num_5);
        
        String num1 = "n" + num_1; 

What I can't figure out is how to take the value in num1 to select one of the Strings named the same thing. I need to do that for all five selected random numbers.

I'm sure there's a way to do this, but it's my first year in a CS class and I can't seem to find the answer anywhere.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Here is a suggestion.

  • put all the words in a List<String>
    (e.g. List<String> = new ArrayList<>())
  • Use Collections.shuffle to shuffle the list.
  • then take the first 5 entries.

Doing the above will guarantee you won't repeat any word unless there are duplicates in the list.

Figuring out how to average the lengths is up to you.

about 4 years ago · Juan Pablo Isaza Relatório

0


package RandomLyric;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class RandomLyric 
{

    public static void main(String[] args) 
    {
        List<String>bob = new ArrayList<>();
        
        bob.add("I");
        bob.add("look");
        bob.add("and");
        bob.add("stare");
        bob.add("so");
        bob.add("deep");
        bob.add("in");
        bob.add("your");
        bob.add("eyes");
        bob.add("I");
        bob.add("touch");
        bob.add("on");
        bob.add("you");
        bob.add("more");
        bob.add("and");
        bob.add("more");
        bob.add("every");
        bob.add("time");
        bob.add("when");
        bob.add("you");
        bob.add("leave");
        bob.add("I'm");
        bob.add("begging");
        bob.add("you");
        bob.add("not");
        bob.add("to");
        bob.add("go");
        bob.add("call");
        bob.add("your");
        bob.add("two");
        bob.add("or");
        bob.add("three");
        bob.add("times");
        bob.add("in");
        bob.add("a");
        bob.add("row");
        bob.add("Such");
        bob.add("a");
        bob.add("funny");
        bob.add("thing");
        bob.add("for");
        bob.add("me");
        bob.add("to");
        bob.add("try");
        bob.add("to");
        bob.add("explain");
        bob.add("how");
        bob.add("I'm");
        bob.add("feeling");
        
        
        Collections.shuffle(bob);
        java.util.Iterator<String> itr=bob.iterator();
        String num1_ = itr.next();
        double num1 = num1_.length();
        System.out.printf( num1_ + ": %.0f",+ num1);
        System.out.println("");
        
        String num2_ = itr.next();
        double num2 = num2_.length();
        System.out.printf( num2_ + ": %.0f",+ num2);
        System.out.println("");
        
        String num3_ = itr.next();
        double num3 = num1_.length();
        System.out.printf( num3_ + ": %.0f",+ num3);
        System.out.println("");
        
        String num4_ = itr.next();
        double num4 = num4_.length();
        System.out.printf( num4_ + ": %.0f",+ num4);
        System.out.println("");
        
        String num5_ = itr.next();
        double num5 = num5_.length();
        System.out.printf( num5_ + ": %.0f",+ num5);
        System.out.println("");
        
        double avg = (num1 + num2 + num3 + num4 + num5)/5;
        System.out.println("The length of the five randomly selected strings is: " + avg);
        
        
        
        
        
        
    }

}

Thank you!!!! I got it all together now!

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda