I can't seem to figure out how to get it to look for any of the random numbers in the array and check if they are divisible by the user input. It just gives me the constant result of
"Number of total divisible inputted numbers: (0)"
import java.util.Random;
import java.util.Scanner;
public class Divisor
{
public static void main (String []args)
{
Scanner keyboard = new Scanner(System.in);
Random rand = new Random();
int[][]strings = new int [4][4];
for (int i = 0; i < strings.length;i++)
{
for(int j = 0; j < strings.length;j++)
{
System.out.println("Please enter the 16 numbers to be checked for division!");
strings[i][j]=keyboard.nextInt();
}
}
int count = 0;
for (int i = 0; i < strings.length;i++)
{
for(int j = 0; j < strings[i].length;j++)
{
strings[i][j] = 1 + (int)(Math.random()*100);
if (strings[i][j]%keyboard.nextInt()==0)
{
count++;
}
System.out.println("Number of total divisible inputted numbers: "+"("+count+")");
}
}
}
}
You may want something like this:
import java.util.Random;
import java.util.Scanner;
public class Divisor
{
public static void main (String []args)
{
Scanner keyboard = new Scanner(System.in);
Random rand = new Random();
int[][]strings = new int [4][4];
int input = 0;
// ask the user for input
System.out.println("Please enter the 16 numbers to be checked for division!");
input=keyboard.nextInt();
// generate random numbers
for (int i = 0; i < strings.length;i++)
{
for(int j = 0; j < strings[i].length;j++)
{
strings[i][j] = 1 + rand.nextInt(100);
//System.out.println(strings[i][j]); // print what is created for testing
}
}
// count divisible numbers for each input
int count = 0;
for (int i = 0; i < strings.length; i++)
{
for (int j = 0; j < strings[i].length; j++)
{
if (input != 0 && strings[i][j] % input == 0) count++;
}
}
System.out.println("Number of total divisible inputted numbers: "+"("+count+")");
}
}
Do you know what your code is doing? Here is a short description:
Make the user input 16 numbers in a array.
System.out.println("Please enter the 16 numbers to be checked for division!");
strings[i][j]=keyboard.nextInt();
Overwrite each number with a random int by Random.nextInt().
strings[i][j] = rand.nextInt();
Compare if a new randomized number (not the same) is divisible by a number the user inputs.
if (rand.nextInt()%keyboard.nextInt()==0)
Do you see the problem?
public class Divisor
{
public static void main (String []args)
{
Scanner keyboard = new Scanner(System.in);
Random rand = new Random();
int[][]strings = new int [4][4]; //Strange calling an array of ints for "strings".
for (int i = 0; i < strings.length;i++)
{
for(int j = 0; j < strings[i].length;j++)
{
strings[i][j] = rand.nextInt(); // Lets add some random number to a 4x4 array.
}
}
int count = 0;
int comparable = keyboard.nextInt(); // We are looking for this number.
for (int i = 0; i < strings.length;i++)
{
for(int j = 0; j < strings[i].length;j++)
{
if (strings[i][j] % comparable == 0) //Does the number match the one we are looking for?
{
count++;
System.out.println("Found a number!");
}
}
}
//We have now looked in the entire array
System.out.println("Number of total divisible inputted numbers: "+"("+count+")");
}
}
Related
I'm building out a user defined array as a game board. The characters used "O" and "." have to be randomized and the "O" has to appear more than once.
This is what I have thus far.
import java.util.Scanner;
public class PacMan {
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Input total rows:");
int row = input.nextInt();
System.out.println("Input total columns:");
int column = input.nextInt();
boolean[][] cookies = new boolean[row+2][column+2];
for (int i = 1; i <= row; i++)
for (int j = 1; j <= column; j++);
cookies [row][column] = (Math.random() < 100);
// print game
for (int i = 1; i <= row; i++)
{
for (int j = 1; j <= column; j++)
if (cookies[i][j]) System.out.print(" O ");
else System.out.print(". ");
System.out.println();
}
}
}
The output, for example, produces a 5 x 5 grid, but the "O" only appears once and is at the bottom right of the grid.
Assistance randomizing the "O" and "." and having the "O" appear in random fashion throughout the board which is initialized by the user input via Scanner.
Here is the updated code which is producing the output that I'm looking for and is user defined.
import java.util.*;
public class PacManTest
{
public static void main(String[] args)
{
char O;
Scanner input = new Scanner(System.in);
System.out.println("Input total rows:");
int row = input.nextInt();
System.out.println("Input total columns:");
int column = input.nextInt();
char board[][] = new char[row][column];
for(int x = 0; x < board.length; x++)
{
for(int i = 0; i < board.length; i++)
{
double random = Math.random();
if(random >.01 && random <=.10)
{
board[x][i] = 'O';
}
else {
board[x][i] = '.';
}
System.out.print(board[x][i] + " ");
}
System.out.println("");
}
}
}
The main issue is the typo in the first loop:
cookies [row][column] = (Math.random() < 100);
should be
cookies [i][j] = (Math.random() < 100);
Second, Math.random() returns a value greater than or equal to 0.0 and less than 1.0 (doc). So, (Math.random() < 100); will always be true. If you want a 50% chance of an O or . use:
cookies[i][j] = Math.random() < 0.5;
Also, not sure what your motivation is for using a starting index of 1 but array indexes start at 0.
I know that this question has been asked before, but not in the the format that I'm writing my code.. Just started taking java classes so I am not familiar with any complex java.. the code below consists of basically all the java I know. Please help! Thanks in advance.
import java.util.Scanner;
public class problem2try {
public static void main(String[] args) {
//declarations
Scanner keyboard = new Scanner (System.in);
int [] inputList = new int [10];
int [] distinctArray = new int [10];
int num;
int counter = 0;
//input
System.out.print("Please enter in 10 integers: ");
for (int i = 0; i < inputList.length; i++)
{
num = keyboard.nextInt();
inputList[i] = num;
}
//processing
distinctArray[0] = inputList[0];
for (int i = 1; i < inputList.length; i++)
{
for (int j = 0; j < inputList.length; j++)
{
if (inputList[i] == inputList[j])
{
counter++;
continue;
}
else
{
distinctArray[i] = inputList[i];
}
}
}
//output
System.out.println("The number of distinct numbers is " + counter);
System.out.print("The distict numbers are: ");
for (int x=0; x<distinctArray.length; x++)
{
if (distinctArray[x] != 0)
System.out.print(distinctArray[x] + " ");
}
}
}
Your logic in the "processing" block seemed off. I modified it to check the current number (outer loop) to all of the known numbers (inner loop). If no match was found, it is appended to the list of known numbers and the count is incremented.
I also modified the "output" code to print the first counter numbers from the list of known numbers. Values past that index are uninitialized.
import java.util.Scanner;
public class problem2try {
public static void main(String[] args) {
//declarations
Scanner keyboard = new Scanner (System.in);
int [] inputList = new int [10];
int [] distinctArray = new int [10];
int num;
int counter = 0;
//input
System.out.print("Please enter in 10 integers: ");
for (int i = 0; i < inputList.length; i++)
{
num = keyboard.nextInt();
inputList[i] = num;
}
//processing
for (int i = 0; i < inputList.length; i++)
{
boolean found = false;
for (int j = 0; j < counter; j++)
{
if (inputList[i] == distinctArray[j])
{
found = true;
break;
}
}
if (!found)
{
distinctArray[counter++] = inputList[i];
}
}
//output
System.out.println("The number of distinct numbers is " + counter);
System.out.print("The distict numbers are: ");
for (int x=0; x<counter; x++)
{
System.out.print(distinctArray[x] + " ");
}
}
}
so how would you find the largest and smallest number here?
import java.util.Scanner;
public class Loops2 {
public static void main (String args[]) {
Scanner input = new Scanner(System.in);
System.out.println("Type 10 numbers");
for (int i = 0; i < 10; i++) {
int number = input.nextInt();
System.out.println(number);
}
}
}
Assuming that what you are trying to do is find the largest and smallest integers are in an array of integers:
public static void main (String args[]) {
Scanner input = new Scanner(System.in);
System.out.println("Type 10 numbers");
//I will create the array here...
int[] nums = new int[10];
//assigning numbers/ints
for(int i = 0; i < 10; i++) {
nums[i] = input.nextInt();
}
//now to find the largest and smallest (in this order)
int largest = 0;
for(int j = 0; j < nums.length; j++)//usage of the 1-line rule :)
if(nums[j] > largest)
largest = nums[j];
int smallest = largest;
//I'm doing this, so that it keeps checking for something lower than the largest number...
for(int k = 0; k < nums.length; k++)//usage of the 1-line rule again :)
if(nums[k] < smallest)
smallest = nums[k];
System.out.println("Largest: " + largest);
System.out.println("Smallest: " + smallest);
}
Hope this helps!
This is probably the best way to implement it
import java.util.Scanner;
public class FindLargestSmallestNumber {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Type 10 numbers");
//array of 10 numbers
int numbers[] = new int[10];
for (int i = 0; i < 10; i++) {
int number = input.nextInt();
System.out.println(number);
numbers[i] = number ;
}
//assign first element of an array to largest and smallest
int smallest = numbers[0];
int largetst = numbers[0];
for(int i=1; i< numbers.length; i++)
{
if(numbers[i] > largetst)
largetst = numbers[i];
else if (numbers[i] < smallest)
smallest = numbers[i];
}
System.out.println("Largest Number is : " + largetst);
System.out.println("Smallest Number is : " + smallest);
}
}
import java.util.Scanner;
public class LargestSmallestNumbers {
private static Scanner input;
public static void main(String[] args) {
int count,items;
int newnum =0 ;
int highest=0;
int lowest =0;
input = new Scanner(System.in);
System.out.println("How many numbers you want to enter?");
items = input.nextInt();
System.out.println("Enter "+items+" numbers: ");
for (count=0; count<items; count++){
newnum = input.nextInt();
if (highest<newnum)
highest=newnum;
if (lowest==0)
lowest=newnum;
else if (newnum<=lowest)
lowest=newnum;
}
System.out.println("The highest number is "+highest);
System.out.println("The lowest number is "+lowest);
}
}
List<Integer> list = new ArrayList<Integer>();
Scanner input = new Scanner(System.in);
System.out.println("Type 10 numbers");
for (int i = 0; i < 10; i++) {
int number = input.nextInt();
System.out.println(number);
list.add(number);
}
Collections.sort(list);
System.out.println("the small: "+ list.get(0));
System.out.println("the big: "+list.get(list.size() - 1));
So I have to fill a 2D array with chars, print out the array, let people search for words, and then print out the number of instances of that word and the array with the instances of that word highlit.
here is my code so far:
import java.util.Scanner;
import java.util.*;
public class testSearchMatrix {
public static void printArray(char[][] myArray){
for(int i = 0; i < myArray.length; i++){
for(int j = 0; j < myArray.length; j++){
System.out.print(myArray[i][j] + " ");
}
System.out.println();
}
}
public static void searchArray(char[][] a){
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter a query to search: ");
String query = keyboard.next();
int queryNum = 0;
int w = 0;
for(int i = 0; i < a.length; i++){
for(int j = 0; j < a[i].length; j++){
if(a[i][j] == query.charAt(w)){
queryNum += 1;
}
}
}
System.out.println(queryNum);
}
public static void main(String[] args){
Scanner keyboard = new Scanner(System.in);
Random random = new Random();
//Create an alphabet array so I can use this to fill in the searchBox array
char[] alphabet = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
System.out.println("Please choose an array size: ");
int a = keyboard.nextInt();
//Create a square array
char[][] searchBox = new char[a][a];
//Fill in the array with random chars
for(int i = 0; i < searchBox.length; i++){
for(int j = 0; j < searchBox[i].length; j++){
int randNum = random.nextInt(25);
searchBox[i][j] = alphabet[randNum];
}
}
//Implement my method to print the array to the screen
System.out.println("Here is the square matrix with random letters: ");
printArray(searchBox);
System.out.println("Enter a query to search: ");
searchArray(searchBox);
}
}
This will print out my array but I can't seem to get the search to work.
Modified Your searchArray function
public static void searchArray(char[][] a){
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter a query to search: ");
String query = keyboard.next();
int queryNum = 0;
String out = null;
int w = 0;
for(int i = 0; i < a.length; i++){
for(int j = 0; j < a[i].length; j++){
if(a[i][j] == query.charAt(w)){
//System.out.println(i+":"+j+a[i][j]);
//w+=1;
if(out==null)
{
out=String.valueOf(a[i][j]);
}else
out=out+a[i][j];
for(int f = 1; f < query.length(); f++){
if(j+f<5){
if(a[i][j+f] == query.charAt(w+f)){
// System.out.println(i+"Index:w+f"+w+f+query.charAt(w+f)+"query.charAt(w+f)Index"+query.indexOf(query.charAt(w+f)));
// System.out.println(i+":"+j+a[i][j+f]);
out=out+a[i][j+f];
System.out.println(out+":"+query+"here"+out.length()+query.length());
if(out.equals(query))
{
System.out.println("Seach Found ");
queryNum += 1;
out=null;
}
}
}
} if(out!=null)
if(out.equals(query))
{
System.out.println("Seach Found ");
queryNum += 1;
out=null;
}
out=null;
}
}
}
System.out.println(queryNum);
}
OuptPut
package test1;
import java.util.Scanner;
public class Question2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int traincars;
int maxweight;
int count = 0;
int total = 0;
maxweight = input.nextInt();
traincars = input.nextInt();
int[] trains = new int[traincars];
for(int i = 0; i < traincars; i++)
{
trains[i] = input.nextInt();
}
if (total < maxweight)
{
for(int i = 0; i < traincars; i++)
{
total = trains[i] + trains[i+1] + trains[i+2] + trains[i+3];
count++;
}
}else
{
count = count + 3;
}
System.out.println("count");
}
}
this is a really simple program but for some reason, the array for the traincars goes out of bounds..
Why is this happening?
The problem is here:
for(int i = 0; i < traincars; i++)
{
total = trains[i] + trains[i+1] + trains[i+2] + trains[i+3];
count++;
}
When i equals traincars-1 you will be accessing elements i+1, i+2. and i+3 which are out of bounds of your trains array.
If your logic is calling for calculating totals of 4 consecutive elements of the array then your for loop should stop earlier:
for(int i = 0; i < traincars - 3; i++) {...}
In the last iteration of
for(int i = 0; i < traincars; i++)
{
total = trains[i] + trains[i+1] + trains[i+2] + trains[i+3];
count++;
}
You try to access trains[i+1] and this is bigger than the length of your trains array.
To make this for loop matter you should just do the following:
for(int i = 0; i < traincars; i++)
{
total += trains[i]; //unless of course you need something else...
count++;
}