I am trying to make this attact a do/while loop repeat itself about 11,688,054 times [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 days ago.
Improve this question
here is my code. I tried to repeat the system.p but no luck
import java.util.Collections;
import java.util.ArrayList;
public class REALLOTTOPROGRAM12 //Declare the class
{
public static void main(String[] args)
{
//To display set of seven numbers between 1 - 69
ArrayList<Integer> numbers = new ArrayList<Integer>();
for (int i = 0; i < 69; i++)
{
numbers.add(i + 1);
}
Collections.shuffle(numbers);
System.out.print("This week's numbers are: "); //This displays the first 5 set of random numbers
for (int j = 0; j < 5; j++)
{
System.out.print(numbers.get(j) + " ");
}
//To display a powerball number between 1 - 26
ArrayList<Integer> pNum = new ArrayList<Integer>();
for (int p = 0; p < 26; p++)
{
pNum.add(p + 1);
}
Collections.shuffle(pNum);
System.out.print("The powerball number is: "); //This displays the random powerball number
for (int q = 0; q < 1; q++)
{
System.out.print(pNum.get(q) + " ");
}
}
}

Related

Having problems with a expanding Java Array [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 1 year ago.
Improve this question
I am currently doing an Array in Java that requires for the user to input a number which leads to an expanding array without the use of a conditional loop like if...else or switch. For example:
If user entered 2:
Output : 2 2 2
2 1 2
2 2 2
or if user entered 3:
Output: 3 3 3 3 3
3 2 2 2 3
3 2 1 2 3
3 2 2 2 3
3 3 3 3 3
This is what I have gotten so far but it breaks the rule of no conditional if...else.
import java.util.*;
public class Test2
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int choice;
String space= " ";
System.out.println("Please Enter an input : ");
choice=input.nextInt();
int adapt[];
int num=choice;
int increment=choice-1;
if(choice==2)
{
for(int i=0; i<=choice; i++)
{
for(int j=0; j<choice-increment; j++)
{
System.out.print(space + (choice) + space);
}
for(int j=choice-increment; j<choice; j++)
{
if(i==choice-increment)
{
System.out.print(space + (choice-increment) + space);
}
else
System.out.print(space + (choice) + space);
}
for(int j=choice-increment; j<choice; j++)
{
System.out.print(space + (choice) + space);
}
System.out.println();
}
}
}
}
The phrasing of the question is confusing. Inspecting your code it looks like you are trying to draw a square grid of integers, where the digits increment radially from the central digit (1) in the middle of the grid. This is a typical programming question which is designed to teach you about loops and indices. Thus your question is really not Java related, however I have given you a working solution in Java below.
Conditional statements are not necessary to solve this problem. The only language construct required is a for loop, specifically a double-nested for loop since this is a two-dimensional problem.
public class Grid {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please Enter an input : ");
final int p = input.nextInt();
// square grid with side length 2p + 1
final int[][] grid = initGrid(2 * p + 1);
fillGrid(grid, p);
printGrid(grid);
}
private static int[][] initGrid(int size) {
// 2-d int array
int[][] grid = new int[size][];
for (int i = 0; i < size; i++) {
grid[i] = new int[size];
}
return grid;
}
private static void fillGrid(int[][] grid, int p) {
for (int row = 0; row < grid.length; row++) {
for (int col = 0; col < grid[row].length; col++) {
grid[row][col] = calculateValue(row, col, p);
}
}
}
private static int calculateValue(int row, int col, int centre) {
// the algorithm for calculating the value of the cell
int dx = Math.abs(row - centre);
int dy = Math.abs(col - centre);
return 1 + Math.max(dx, dy);
}
private static void printGrid(int[][] grid) {
for (final int[] row : grid) {
for (int cell : row) {
System.out.print(String.format(" %2d ", cell));
}
System.out.println();
}
}
}

How do I recursively count up to less than n [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 2 years ago.
Improve this question
I am struggling with the problem of having applications of loops and arrays.
I have a variable "n" which represents the limit of the loop, i.e.
if n = 3, the array would look like:
arr[1,2,3,1,2,3,1,2,3];
or if n = 4, it would look like:
arr[1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4];
here's my code so far, someone please let me know the mistake I have made in implementing the above problem...
public static void main(String[] args) {
countingUp(3);
}
public static void countingUp(int n) {
int[] arr = new int[n * n];
int k = n;
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
arr[i] = n ;
}
}
System.out.println(Arrays.toString(arr));
}
This is the major mistake you have done...
arr[i] = n ;
You should update value after each interval of length n which can be controlled by the loop running with i and the value inside each interval could be controlled with the loop j. See that one change I have made in the code below...
public static void main(String[] args) {
countingUp(3);
}
public static void countingUp(int n) {
int[] arr = new int[n * n];
int k = n;
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
arr[i*n+j] = j+1 ;
}
}
System.out.println(Arrays.toString(arr));
}

Java double input in array [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 5 years ago.
Improve this question
I have question about array input.I have to create program which will enter number n (number of students) and then i have to input index of student,and score.Score has to be from the highest from the lowest.I have made that but problem is that my index won't "follow" my score.Ex.
n = 3
Input first number is index,second score!
1 - 2
2 - 4
3 - 5
Output :
1 - 5
2 - 4
3 - 2
Well problem is that my index will not appear with my score
My code is in picture with this:Picture
package danl;
import java.util.Scanner;
public class Nizovi6 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int[] index = new int[n];
int[] score = new int[n];
for (int i = 0; i < n; i++) {
index[i] = scanner.nextInt();
score[i] = scanner.nextInt();
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (score[i] > score[j]) {
int t = score[j];
score[j] = score[i];
score[i] = t;
}
}
}
for (int i = 0; i < n; i++) {
System.out.println(index[i] + " " + score[i]);
System.out.println();
}
}
}
Because of you didn't swap index[] array:
t = index[j];
index[j] = index[i];
index[i] = t;
When you swap your elements in score array, do a corresponding swap in index array
int t;
if (score[i] > score[j]) {
t = score[j];
score[j] = score[i];
score[i] = t;
t = index[j];
index[j] = index[i];
index[i] = t;
}
NOTE: There are a lot of other better ways to handle/solve this by using appropriate data structure for this.
You can create a separate class for having the score and its position
private static class Score {
private int score;
private int position;
Score(int score, int position) {
this.score = score;
this.position = position;
}
public String toString() {
return position + " " + score;
}
}
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
List<Score> scores = new ArrayList<>();
for (int i = 0; i < n; i++) {
int score = sc.nextInt();
scores.add(new Score(score, i + 1)); //Create new Score objects
}
//Your sorting code here
//OR
//scores.sort((s1, s2) -> s1.score - s2.score); //Sort it by increasing scores
//OR
// scores.sort(Comparator.comparingInt(s -> s.score));
System.out.println(scores);

Creating program for finding minimum and maximum values that can be calculated by summing exactly four of the five integers [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 6 years ago.
Improve this question
process :
initial numbers are 1,2,3,4,5 and .
If we sum everything except 1, sum is 2+3+4+5=14 .
If we sum everything except 2, sum is 1+3+4+5=13.
If we sum everything except 3, sum is 1+2+4+5=12.
If we sum everything except 4, sum is 1+2+3+5=11.
If we sum everything except 5, sum is 1+2+3+4=10.
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int i, j, temp;
int[] a = new int[5];
int[] res = new int[]{0, 0, 0, 0, 0};
System.out.print("Enter 5 number");
for (i = 0; i < 5; i++)
a[i] = in.nextInt();
for (i = 0; i < 5; i++) {
for (j = 0; j < 5; j++) {
if (i != j) {
res[i] = res[i] + a[j];
}
}
}
for (i = 0; i < 4; i++) {
for (j = i + 1; j < 5; j++) {
if (res[i] > res[j]) {
temp = res[i];
res[i] = res[j];
res[j] = temp;
}
}
}
System.out.print(a[0] + " " + a[4]);
}
}
Output: 1 5
Expected Output : 10 14
I have forgotten something in this code. Please guide me to resolve this problem.
You're getting Output 1 5 because you're using the contents of the int a[] array, when instead you want to use the int res[] array, which contains the sorted sums.
Change the last line:
a[0] + " " + a[4] to res[0] + " " + res[4]

two dimensional string[] convert to string and output = matrix [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have written the code:
public static void main(String[]args)
{
Scanner input = new Scanner(System.in);
System.out.print("Type your text: ");
String text = input.nextLine();
int counter = text.length();
if(text.length()> 16)
{
System.out.println("Error: input text is greater than 16 characters");
System.exit(0);
}
else
{
while(counter < 16)
{
text = text.concat("x");
counter++;
}
char[][] k = new char[4][4];
int push = 0;
for(int i = 0; i < k.length; i++)
{
for(int j = 0; j < k[i].length; j++)
{
k[i][j] = text.charAt(j+ push);
System.out.print(k[i][j] + " ");
}
System.out.println();
push = push + 4;
}
}
}
And input is: abcdefghijklm
output is:
a b c d
e f g h
i j k l
m x x x
So all i want is, if i type: abcdefghijklm
I want this output:
a e i m
b f j x
c g k x
d h l x
To get your desired output, you're going to need to print out the matrix after populating it (you're also going to have to loop through it in a different way). Right now, you're adding each character to the matrix and immediately printing it, so you will print it in the same order as the input string (with extra line breaks).
So you want something like:
for(int i = 0; i < k.length; i++) {
for(int j = 0; j < k[i].length; j++) {
k[i][j] = text.charAt(j+ push);
}
push = push + 4;
}
for(int i = 0; i < k.length; i++) {
for(int j = 0; j < k[i].length; j++) {
System.out.print(k[j][i]+" ");
}
System.out.println();
}
You simply need a transpose operation for your matrix. Just use two nested loops, and switch the indices. i.e. instead of k[i][j], use k[j][i].

Categories

Resources