Finding a certain random value in which row and how many times has been appeared
1 次查看(过去 30 天)
显示 更早的评论
Hello there we have a semester exercise in java and i would like a help, we want a program that stores a table of 20 random integer values and then asks the user to give a value from 0 to 20 and then to show if that value is among them how many times has appeared and in which positions
import java.io.*;
import java.util.Random;
class anazitisi // research
{
public static void main(String[]args) throws IOException
{
InputStreamReader reader = new InputStreamReader(System.in);
BufferedReader Input = new BufferedReader(reader);
int[] x = new int[20];
Random rndm = new Random();
int i= rndm.nextInt();
for( i=0; i<20; i++)
x[i] = rndm.nextInt(20);
for( i=0; i<20; i++)
System.out.println("x["+i+"]="+x[i]);
System.out.println("Δώσε έναν αριθμό απο το 1 μέχρι το 20"); // give a value from 1 until 20
String AR1 = Input.readLine();
int b = Integer.parseInt(AR1);
System.out.println("O αριθμός" +x.length+ "βρέθηκε φορές στις θέσεις "); // the number....has been found x time in the positions.....
}
it's in greek but i hope you get the gist somehow any help please until 10/1 would be welcome i know it's very limited time ...Thanks in advance
0 个评论
回答(1 个)
KALYAN ACHARJYA
2023-1-3
This is a Matlab forum, Matlab answer, try to replicate in other langauges-
rand_data=randi([0 20],[1, 20])
num=input('Enter any number (0 to 20): ')
times_num=sum(rand_data==num)
[r c]=find(rand_data==num)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!