Mới học Java cần giúp đỡ :D

tuan_an04

Donkey Kong
Mình mới học java.Bây giờ mình muốn thử nhập 1 ma trận kích thước mxn rồi in ra màn hình
Ví dụ như : 1 2 3 4 5
aaaaaaaaaa
2 3 3 1 4
aaaaaaaaaa1 3 2 6 5
aaaaaaaaa .................
.Nhưng hình như java nó có chế độ tự xuống dòng thì phải nên mình ko làm nó hiển thị ra được.Hoặc cũng có thể là viết sai code:)
Đây là code của mình
package project1;
import java.util.Scanner;
/**
*
* @author Ngo Tuan An
*/
public class Matrix {
aapublic static void InputMatrix(double a[][],int row,int col,Scanner in){
aaa for(int i=0;i<row;i++)
aaaaa for(int j=0;j<col;j++)
aaaaa {
aaaaa System.out.print("a["+(i+1)+ "]["+(j+1)+ "]=");
aaaaa a[j]=in.nextDouble();
aaaaa }
}
aa public static void OutputMatrix(double a[][],int row,int col){

aaaa for(int i=0;i<row;i++)
aaaaa for(int j=0;j<col;j++)
aaaaa {
aaaaa System.out.printl(a[j]+" ");
aaaaa System.out.print("\n");
aaaaa }
}
aa public static void main(String args[])
aaa {
aaaa double a[][];
aaaa a = new double[20][20];
aaaa int row,col;
aaaa Scanner in = new Scanner(System.in);
aaaa System.out.print("Nhap vao so hang cua ma tran:");
aaaa row = in.nextInt();
aaaa System.out.print("Nhap vao so cot cua ma tran:");
aaaa col = in.nextInt();
aaaa InputMatrix(a,row,col,in);
aaaa System.out.print("\n-----------------------------");
aaaa System.out.print("\nMa tran vua nhap vao la:\n");
aaaa OutputMatrix(a,row,col);
aaaa }
}



Mấy cái aaa màu trắng để cho nó lùi dòng vào cho dễ nhỉn :D
Còn đây là hình sau khi chạy chương trình =((


Ai biết xin chỉ dùm :):):):):):)
 
Chưa hiểu ý bạn là cần xuất như thế nào ?
Tại Heo thấy kiểu double thì góp ý thôi :P
 
for(int i=0;i<row;i++)
for(int j=0;j<col;j++)
{
------System.out.printl(a[j]+" ");
------System.out.print("\n");
}


Cái này ko phải nè ... vì trong cái for thì xuất 1 số rồi xuống 1 dòng rồi lại for nên ko như ý bạn đc
Nên đưa dòng xuất \n ra khỏi cái for j
 
Cái này ko phải nè ... vì trong cái for thì xuất 1 số rồi xuống 1 dòng rồi lại for nên ko như ý bạn đc
Nên đưa dòng xuất \n ra khỏi cái for j

đúng đó: lúc đầu đọc lướt wa thấy println nên nói luôn mà ko để ý vòng lặp for ::)
aaaa for(int i=0;i<row;i++)
aaaa {
aaaaa for(int j=0;j<col;j++)
aaaaa {
aaaaa System.out.print(a[j]+" ");
aaaaa }
aaaaa System.out.print("\n");
aaaa }
 
Bạn cho Heo nick yahoo đi ... có gì giúp nhau học tập nào ::)
Học kì này cũng vừa đăng kí học java :P
 
...bà con có ai biết làm cái đề này ko T_T...học thiết kế mà bắt làm it khổ quá T_T...

(a) Writing a Part class (50 marks)

ABC Bicycle Parts specializes in supplying various bicycle parts for the Rali Bicycles. You are
required to write a class named Part to keep track of stock-level for the various parts. This class should
provide the following methods to support the specified operations

int getStockLevel() // to return the current stock level
void replenish(int qty) // to add to the stock level the qty specified
double supply(int qty) // to work out and return the cost of supplying
// specified qty if stock available and -1.0 otherwise.

This class should have instance variables (data members) to store the ID, name, stock-level and unit-
price and a constructor taking values for all of the instance variables. This class should provide
accessors for all the instance variables. You are expected to test your Part class with the TestPart class
below.
COSC2081 – 2009A – Assignment 3

- 2 -
Test class

public class TestPart{
public static void main(String args[]) {
// Part p122 has unit-price of 0.10 and current level = 5000
Part part = new Part("p122", "Chain", 5000, 0.10);

part.replenish(1000); // replenishing stock by 1000 parts
part.replenish(500); // replenishing stock by 500 parts

for (int i=0; i<2; i++){
int qty = 6000; // attempting to supply 6000 parts
double amount = part.supply(qty);
if ( amount > 0 )
System.out.println(part.getID() +
" "+ qty +" supplied. " + " cost = " + amount);
else
System.out.println(part.getID() +
" "+ qty +" items not available. ");
System.out.println("Available qty = " +
part.getStockLevel());
}
}
}

Expected Output
p122 6000 supplied. cost = 600.0
Available qty = 500
p122 6000 items not available.
Available qty = 500
Attempt to supply failed COSC2081 – 2009A – Assignment 3

- 3 -
(b) Using the Part class (50 marks)

(i) Declare an array that can store the references of up to 5 Part objects constructed with the data (ID,
name, stock-level and unit-price) below.

p122, Chain, 48, 12.5
p123, Chain Guard, 73, 22.0
p124, Crank, 400, 11.5
p125, Pedal, 38, 6.5
p126, Handlebar, 123, 9.50

(ii) Now allow the users to either replenish or supply parts repeatedly. If the user wants to replenish,
the letter R should be followed by ID and quantity. If the user wants to supply the letter S should
be followed by ID and quantity. If a nonexistent ID is entered, display an appropriate error
message. Below is sample input and output: The underlined text is user input.

Please enter either S (supply) or R (replenish) followed by ID and quantity.

R p122 10
New Stock-level for p122 (Chain) is 58
S p125 20
New Stock-level for p125 (Pedal) is 18
S p905 20
No item found with ID p905
R p122 30
New Stock-level for p122 (Chain) is 88
Empty String (to terminate)


(iii) Display the final stock level for all the parts.
 

Attachments

Cái bài này là muốn làm gì vậy ?
Nếu cho biết cái đề muốn gì thì may ra Heo giúp đc, chứ tiếng Anh ko thì lười quá :P
 
ặc... asignment môn java trường RMIT phải ko....
sao ko lo tự làm mà còn đem zo đây hỏi....haha.. coi chừng bị đá 1 cái là fail immediately ah....
cái nì chắc java 1... đề dễ thế ráng tự làm đi...không hiểu thì lôi đầu cha thày ra hỏi ngay...
đề này không ráng làm thì lên java 2 là tiu ngay ah....
 
Khó thì phải hỏi là đúng rồi. Không thì việc gì phải nhờ các cao thủ.:D
 
Back
Top