Outputting specific values from an two dimensional array [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Problem with output from a two dimensional array, basically 8 rows and 5 columns, each row represents one athlete, the five colums represent the points they have gained in each task. I have to find out If an athlete went beyond 250 points in a single task. Firstly, I made an integer which counts the number of times the Array has been look at, If the number can be divided by 5 it means the next number is going to be the next row and thus a new athlete, next I created another loop to check all the previous numbers and if any of them goes beyond 250, I add one number to the total + break the cycle. The result I have been getting back is 0, or before I got nothing.
for (i=0; i<8; i++) {
for (j=0; j<5; j++)
if(rezultati[i][j]>0) {
skaits = skaits + 1;
}
else if(skaits % 5==0) {
for (int a=0; a<5; a++) {
if(rezultati[i-a][j-a]>250) {
daudzums = daudzums + 1;
break;
}
}
}
}
realised that the row index should be just i if(rezultati[i][j-a]>250), didnt fix the issue tho

You already have an outer loop that will move to the next row/athlete for you. Simply look at the current value and if it exceeds 250, increment your counter and move to the next athlete with break:
int daudzums = 0;
int[][] rezultati = new int[8][5];
// ... populate zezultati somehow ...
for (int i=0; i<rezultati.length; i++) {
for (int j=0; j<rezultati[i].length; j++) {
if (rezultati[i][j]>250) {
daduzums++;
break; // move to next athlete
}
}
}
System.out.println("daudzums = " + daudzums);
After the loops, daudzums will tell you how many athletes had at least one task over 250.

Related

Print occurrences of elements in an array list [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I need two input array A and array B.
The answer will be like this - if input A is
{5,2,6,8}
and input B is
{6,5,5,8,5,6}
then the output will be:
2 (0 times), 5 (3 times), 6 (2 times), 8 ( 1 times)
Here is an example of how you can achieve it.
public static void main(String[] args) {
int[] A={5,2,6,8},
B={6,5,5,8,5,6};
int[] count = new int[A.length]; // An array counting the occurences
StringBuilder result = new StringBuilder(); // The String to be displayed. StringBuilder is better than String, when multiple concatenations occur.
Arrays.sort(A); // Sorts array A in ascending order (by default).
// This loop counts the occurence of each value of A inside B
for (int i = 0; i <A.length; i++){
for (int j = 0; j < B.length; j++){
if (A[i] == B[j])
count[i]++;
}
// After each value of A, add the number of occurence in the String
result.append(A[i]).append(" (").append(count[i]).append(" times), ");
}
// Displays the String in the console, removing the last ", "
System.out.println(result.substring(0,result.length()-2));
}

Upside Down Isosceles Triangle [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Answered:
The assignment I was given is to write a Java program that outputs an isosceles triangle based on the users input. For instance, if a user were to input the number 5 after being prompted, the program would output
*****
****
***
**
*
We were instructed to use while loops, but I was not having any success with that. I decided to use for loops instead but am still having trouble. I've declared my variables and have prompted the user for input. Below you will find my for loops. Please help! All my program is doing is printing out a continuous column of pound signs.
//For loop
for (CountOfRows=UserInput; CountOfRows>0; CountOfRows--)
{
System.out.println("# ");
for (CountOfColumns=UserInput; CountOfColumns>0; CountOfRows++)
{
System.out.println("# ");
}
}
If you'd like to use a while loop, you can simply do this:
while(num > 0){ //stay in loop while the number is positive
int temp = num; //make a copy of the variable
while(temp-- > 0) //decrement temp each iteration and print a star
System.out.print("*"); //note I use print, not println
System.out.println(); //use println for newline
num--; //decrement number
}
You need to change your inner for loop so that it runs until the end of the index from the first for loop like this:
int num = 5;
for (int i = num; i > 0; i--) {
for (int j = 0; j < i; j++) {
System.out.print("*");
}
System.out.println();
}

Writing a pattern in java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
2- (Display patterns) Write a method to display a pattern as follows:
1
2 1
3 2 1
…
n n-1 … 3 2 1
The method signature is public static void displayPattern(int n). the user enters number for how long they want the pattern to be.
I know how to set it up but don't know what code to use inside the method. This is how far I got-
Scanner input=new Scanner(System.in);
System.out.println("Please enter the num for how long the pattern is");
int n= input.nextInt();
}
public static void TheNum(int n){
}
for(int i=0; i <= n; i++) {
for(int j=i; j > 0; j--) {
System.out.print(j + " ");
}
System.out.println();
}
In the above loops. First loop goes from 0 to n times depending on value of n. Second loop is goes from value of i and keeps looping as long as j > 0.
First loop i=0 prints nothing, for i=1 j prints 1, only once then come out of loop and i become 2 and the inner loop prints 2 and 1 and then comes out of loop and prints new line and i become 3 and then inner loop print 3 and 2 and 1 and then come out of loop and so on.
Put above code in your method TheNum and then call your method passing it value of n that is provided by user as below:
TheNum(n);
Put above function call after the statement that takes the input.

List the factors of an integer [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
public int[] factors(int n) {
int count[] = new int[n];
for (int i = 1; i <= count.length; i++) {
count[i] = (count.length % i);
}
return count;
}
}
Anyone have an idea how to list all the factors?
set int i = 0; as array starts from 0
Factor can only be considered if value % i==0 so add if condition
You need one more variable to increment array Index as you can't use i for that.So add one more variable to increment array index to add factor.
Think more on your code, debug especially if possible,check out the values ,add print statements if needed to see what's going on and that's it you will definitely win the task.
Other than that if you can use SOF you can use Google as well :)
You should use a List instead of array. Since you can't initialize the array without knowing the size.
Then you can try something like following
public List<Integer> factors(int n) {
List<Integer> list = new ArrayList<>(); // initialize the list
for (int i = 1; i <= n; i++) {
if (n % i == 0) { // decide the factor.
list.add(i); // now i is a factor, needs to add to list
}
}
return list; // return list contains factors.
}

Strings - Number guessing game [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
This is a game that generates a random 4 digit number and then lets the user input 5 chances to guess the game and returns a hint as to how many numbers were in the correct place and how many matched but were in the wrong place.
I am having a problem comparing two strings. One of the strings holds the correct answer to the game, the other string holds the most recent 4 digit guess.
aCount needs to be the number of characters that are in correct position.
bCount needs to be the number of characters that are in the correct answer, but not in correct position.
bCount is the part I'm having trouble with (it's not working as I expect). I thought I might try a double for loop, but I'm not sure.
static String getHint(String guess, String answer){
int aCount=0;
int bCount=0;
String hintString="";
for (int i =0; i<answer.length(); i++){
char guessAChar = guess.charAt(i);
char ansAChar = answer.charAt(i);
if(guessAChar == ansAChar){
aCount++;
}
}
for (int indexOfGuess = 0; indexOfGuess < answer.length(); indexOfGuess++)
{
for (int indexOfActualNumber = 0; indexOfActualNumber < answer.length(); indexOfActualNumber++)
{
if (guess.charAt(indexOfGuess) == (answer.charAt(indexOfActualNumber)))
{
bCount++;
}
}
}
bCount = bCount - aCount;
hintString =("Your hint is: \n"+aCount+"A" + bCount + "B");
return hintString;
}
}
EDIT: This problem has been solved: This method now searches and outputs how many numbers in each string match, and how many in each position are the same!
Variable bCount is the amount of numbers in the final string, but not in the right spot. You need a double loop for this to check each character in each string to see if they match. This might point you in the right direction.
int outer_counter = 0;
for(char g : guess.toCharArray())
{
int inner_counter = 0;
for(char a : answer.toCharArray())
{
if(g == a && inner_counter != outer_counter)
{
bCount++;
}
inner_counter++;
}
outer_counter++;
}
The counters make sure these are NOT in the same position.
So bCount is determining the number of digits that are in the answer, but not in the right place. So let's try this:
public string bCount()
{
for (int indexOfGuess = 0; indexOfGuess < answer.length(); indexOfGuess++)
{
for (int indexOfActualNumber = 0; indexOfActualNumber < answer.length(); indexOfActualNumber++)
{
if (guess.charAt(indexOfGuess).equals(answer.charAt(indexOfActualNumber))
{
bCount++;
}
}
}
}
This method does:
Creates an outer loop that loops through each index of the "guess" array
Creates an inner loop that, while the "guess" array is at an index, the inner loop will loop through each index (so while "guess" is at index 0, "answer" will go through all indexes before "guess" goes to 1, and then the inner loop runs again)
Check to see if the number at the guess index = number at the actual index.
If it is, increment bCount.
The only issue which I haven't taken care of for you is if the number is entered twice. Example: if the number is 1900, and you guess 1909, you number for bCount will be higher than it should be. However, I'll leave that to you as this is a homework assignment after all.

Categories