「java乘法类」java的乘法
今天给各位分享java乘法类的知识,其中也会对java的乘法进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
- 1、java 设计一个乘法类Multiplication
- 2、java乘法运算
- 3、用java怎么写矩阵乘法?
- 4、编写java程序九九乘法表用类来实现
- 5、JAVA 两个数相乘怎么写?
- 6、java 怎么算乘法
java 设计一个乘法类Multiplication
public class Multiplication {
/* 计算两个整形数的积 */
public int mul(int i, int j) {
return i * j;
}
/* 计算两个浮点数的积 */
public double mul(double x, double y) {
return x * y;
}
/* 计算三个浮点数的积 */
public double mul(double x, double y, double z) {
return x * y * z;
}
}
……………………………………………………………………………………………
import java.applet.Applet;
import java.awt.Graphics;
/*测试与输出类*/
public class Test extends Applet {
private String re1, re2, re3;
public void init() {
Multiplication m = new Multiplication();
re1 = (new Integer(m.mul(3, 4))).toString();
re2 = (new Double(m.mul(3.3, 4.3))).toString();
re3 = (new Double(m.mul(3.3, 4.3, 5.3))).toString();
}
public void paint(Graphics g) {
g.drawString(re1, 100, 80);
g.drawString(re2, 100, 100);
g.drawString(re3, 100, 120);
}
}
java乘法运算
//早前写的,简易的!
import java.util.*;
public class TestSum {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
while(true){
init();
}
}
private static void init() {
System.out.println("请输入算数表达式如:1+1回车即可:");
String str = sc.nextLine();
if (!str.matches("[^a-zA-Z()^#$@!~]+")) {
System.out.println("输入错误重输:请输入算数表达式如:1+1回车即可:");
str = sc.nextLine();
}
int q = 0, h = 0, sum = 0;
char chs = ' ';
for (int i = 0; i str.length(); i++) {
chs = str.charAt(i);
if (chs == '+' || chs == '-' || chs == '*' || chs == '/') {
q = Integer.valueOf(str.substring(0, i));
h = Integer.valueOf(str.substring(i + 1, str.length()));
switch (chs) {
case '+':
sum = q + h;
break;
case '-':
sum = q - h;
break;
case '*':
sum = q * h;
break;
default:
sum = q / h;
break;
}
break;
}
}
System.out.println("结果="+sum+"\n继续下一轮:");
}
}
用java怎么写矩阵乘法?
import java.util.Scanner;
public class Matrix {
public double[][] create() {
Scanner sc = new Scanner(System.in) ;
System.out.print("请输入矩阵的行高:");
int a = sc.nextInt() ;
System.out.print("请输入矩阵的列宽:");
int b = sc.nextInt() ;
double[][] x = new double[a][b] ;
for(int i=0;ilt;a;i++){
for(int j=0;jlt;b;j++){
System.out.print("请输入元素x["+i+"]["+j+"]的值:" );
x[i][j] = sc.nextDouble() ;
}
}
return x ;
}
public double[][] multiply(double[][] x,double[][] y){
double[][] result = null ;
int a = x[0].length ;
int b = y.length ;
if(a != b){
System.out.println("输入的维数不匹配,不能进行运算");
}else{
int c = x.length ;
int d = y[0].length ;
result = new double[c][d] ;
for(int i=0;ilt;c;i++){
for(int j=0;jlt;d;j++){
double sum = 0 ;
for(int k=0;klt;a;k++){
sum += x[i][k]*y[k][j] ;
}
result[i][j] = sum ;
}
}
}
return result ;
}
public void print(double[][] x){
System.out.println("矩阵为:");
for(int i=0;ilt;x.length;i++){
for(int j=0;jlt;x[i].length;j++){
System.out.print(x[i][j] + " ") ;
}
System.out.println();
}
}
}
测试类:
public class TestMatrix {
public static void main(String[] args) {
Matrix m = new Matrix() ;
//double[][] x = {{1,2},{3,2}} ;
//double[][] y = {{1,2,1},{2,3,3}} ;
System.out.println("创建第一个数组:") ;
double[][] x = m.create() ;
m.print(x) ; //用来验证输入的是否和你一样的,没啥作用
System.out.println("创建第二个数组:");
double[][] y = m.create() ;
m.print(y) ; //用来验证输入的是否和你一样的,没啥作用
double[][] result = m.multiply(x, y) ;
if(result == null){
return ; //如果输入的矩阵不能运算就不输出结果了。
}
m.print(result) ;
}
}
Java是一门面向对象编程语言,不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承、指针等概念,因此Java语言具有功能强大和简单易用两个特征。Java语言作为静态面向对象编程语言的代表,极好地实现了面向对象理论,允许程序员以优雅的思维方式进行复杂的编程。 Java具有简单性、面向对象、分布式、健壮性、安全性、平台独立与可移植性、多线程、动态性等特点。Java可以编写桌面应用程序、Web应用程序、分布式系统和嵌入式系统应用程序等。
编写java程序九九乘法表用类来实现
public class MultiplyTest
{
public static void main(String[] args){
for(int i = 1; i = 9; i++){
for(int j = 1; j = i; j++){
System.out.print(new Member(i, j));
}
System.out.println();
}
}
}
class Member{
int num1;
int num2;
public Member(int num1, int num2){
this.num1 = num1;
this.num2 = num2;
}
public String toString(){
return num1 + "*" + num2 + "=" + num1* num2 + "\t";
}
}
手工写的请采纳
JAVA 两个数相乘怎么写?
public class Day25B {
public static void main(String[] args) {
baiint[] arr1=new int[5],arr2=new int[5],result=new int[5];
for (int i = 0; i result.length; i++) {
arr1[i]=(int)Math.round(Math.random()*40+10);
arr2[i]=(int)Math.round(Math.random()*40+10);
result[i]=arr1[i]*arr2[i];
}
System.out.println("索引\tarr1\tarr2\tresult");
for (int i = 0; i result.length; i++) {
System.out.println(i+"\t"+arr1[i]+" x "+arr2[i]+" =\t"+result[i]);
}
}
}
扩展资料:
javap 类文件反汇编器数据类型boolean 布尔型
byte 字节型
char 字符型
short 短整型
int 整形
long 长整形
float 单精度浮点型
double 双精度浮点型class 类null 空类型
interface 接口
java 怎么算乘法
package CMJqimo;
import java.util.Random;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class test {
static int trueresult = 0;
public static void main(String args[]) {
new test();
}
public test() {
JPanel jp = new JPanel();
JLabel jl = new JLabel();
JTextField jtf;
JFrame jf = new JFrame("Exam of Multiplication");
JButton jb = new JButton();
Container contentPane = jf.getContentPane();
contentPane.add(jp);
contentPane.setLayout(new BorderLayout());
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setSize(300, 200);
jp.setLayout(new FlowLayout());
jp.setBackground(Color.GREEN);
jp.setSize(1000, 1000);
int num1 = random_number();
int num2 = random_number();
trueresult = num1 * num2;
JLabel jll = new JLabel(num1 + " x " + num2 + " =");
jp.add(jll);
jtf = new JTextField(5);
jp.add(jtf);
jb = new JButton("提交");
jp.add(jb);
jl = new JLabel(" ");
jp.add(jl);
contentPane.add(jp);
jf.setLocation(400, 200);
jf.setVisible(true);
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int input = Integer.valueOf(jtf.getText());
String s = output_result(trueresult, input);
JOptionPane.showMessageDialog(jp, s);
int a = random_number();
int b = random_number();
trueresult = a * b;
jll.setText(a + " x " + b + " =");
jtf.setText("");
}
});
}
public static int read_input(String s) {
return Integer.parseInt(s);
};
public static int random_number() {
Random r = new Random();
int num = r.nextInt(10) + 1;
return num;
}
public String output_result(int trueresult, int input) {
Random r = new Random();
if (input == trueresult) {
String[] s = { "Very good", "Excellent", "Great job" };
return s[r.nextInt(3)];
} else {
return "No, please try again";
}
}
}
java乘法类的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java的乘法、java乘法类的信息别忘了在本站进行查找喔。
发布于:2022-11-24,除非注明,否则均为
原创文章,转载请注明出处。