用Java编写一个点菜系统

JavaScript07

用Java编写一个点菜系统,第1张

import java.util.*

public class s {

static int x,xx,y,yystatic double sum=0

public static void main(String[] args)

{

String answer="y"double h=0

Scanner input=new Scanner(System.in)

System.out.println("请输入客户数:")

int m=input.nextInt()

int n=m*3

tostring()

do

{

order()

h+=(s(x,xx))+(d(y,yy))

System.out.println("是否继续:")

answer=input.next()

if(answer.equals("n"))

break

}while(true)

System.out.println(h+n)

}

static void tostring()

{

System.out.println("菜单")

System.out.println("食品:")

System.out.println("1咖喱牛肉饭¥40.00")

System.out.println("2寿司套餐¥65.00")

System.out.println("3扬州市消防3米¥45.00")

System.out.println("4沙朗牛排和意大利面¥72.00")

System.out.println("5鸡肉蔬菜卷¥42.00")

System.out.println("饮料:")

System.out.println("21软饮料¥10.00")

System.out.println("22红葡萄酒¥15.00")

System.out.println("23啤酒¥15.00")

}

static double s(int f,int x)

{

double p=0

switch(f)

{

case 1:p=40.00break

case 2:p=65.00break

case 3:p=45.00break

case 4:p=72.00break

case 5:p=42.00break

}

sum=p*x

return sum

}

static double d(int f,int x)

{

double p=0

switch(f)

{

case 21:p=10.00break

case 22:p=15.00break

case 23:p=15.00break

}

sum=p*x

return sum

}

static void order()

{

Scanner input=new Scanner(System.in)

System.out.println("请选择食品:")

x=input.nextInt()

System.out.println("请选择份数:")

xx=input.nextInt()

System.out.println("请选择饮料:")

y=input.nextInt()

System.out.println("请选择瓶数:")

yy=input.nextInt()

}

}

加分!!!!!

简单的点菜系统,可供学习:

#include <stdio.h>

#include <stdlib.h>

#include <errno.h>

#include <string.h>

#include <netdb.h>

#include <sys/types.h>

#include <netinet/in.h>

#include <sys/socket.h>

#define SERVPORT 3333

#define MAXDATASIZE 100 /*每次最大数据传输量 */

int main(int argc, char *argv[])

{

int sockfd, recvbytes

char buf[MAXDATASIZE]

struct hostent *host

struct sockaddr_in serv_addr

if (argc <2)

{ fprintf(stderr,"Please enter the server's hostname!\

")exit(1) }

if ((host=gethostbyname(argv[1]))==NULL)

{ perror("gethostbyname出错!")exit(1)}

if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)

{ perror("socket创建出错!")exit(1) }

//初始化客户端

serv_addr.sin_family=AF_INET

serv_addr.sin_port=htons(SERVPORT)

serv_addr.sin_addr = *((struct in_addr *)host->h_addr)

bzero(&(serv_addr.sin_zero),8)

//connect

if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(struct sockaddr)) == -1)

{ perror("connect error!")exit(1)}

//recv

if ((recvbytes=recv(sockfd, buf, MAXDATASIZE, 0)) ==-1)

{ perror("recv出错!")exit(1)}

buf[recvbytes] = '\\0'

printf("Received: %s",buf)

close(sockfd)

return 0

}

客户端#include <stdio.h>

#include <stdlib.h>

#include <errno.h>

#include <string.h>

#include <sys/types.h>

#include <netinet/in.h>

#include <sys/socket.h>

#include <sys/wait.h>

#define SERVPORT 3333 /*服务器监听端口号 */

#define BACKLOG 10 /* 最大同时连接请求数 */

int main()

{

int sockfd,client_fd,sin_size/*sock_fd:监听socket;client_fd:数据传输socket */

struct sockaddr_in my_addr/* 本机地址信息 */

struct sockaddr_in remote_addr/* 客户端地址信息 */

//创建一个套接字,PF_INET,流式,

if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)

{ perror("socket")exit(1) }

//初始化服务端

my_addr.sin_family=AF_INET

my_addr.sin_port=htons(SERVPORT)

my_addr.sin_addr.s_addr = INADDR_ANY

bzero(&(my_addr.sin_zero),8)

//将套接字地址与所创建的套接字号联系起来

if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1)

{ perror("bind") exit(1) }

//愿意接收连接

if (listen(sockfd, BACKLOG) == -1)

{ perror("listen") exit(1) }

while(1)

{

sin_size = sizeof(struct sockaddr_in)

if ((client_fd = accept(sockfd, (struct sockaddr *)&remote_addr, &sin_size)) == -1)

{ perror("accept") continue}

printf("received a connection from %s\

", inet_ntoa(remote_addr.sin_addr))

if (!fork()) { /* 子进程代码段 */

if (send(client_fd, "Hello, you are connected!\

", 26, 0) == -1)

perror("send")close(client_fd)exit(0)}

close(client_fd) }

return 0

}